手机版

struts2 拦截器开发的总结

时间:2025-05-14   来源:未知    
字号:

一、如何获取异常信息的String类型信息二、C标签if语句的写法三、设置全局错误页面四、如何为div赋值五、自定义拦截器的使用

一、如何获取异常信息的String类型信息

(1)java类的写法

try {

result = invocation.invoke();

} catch (Exception e) {

StringWriter sw=new StringWriter();

PrintWriter pw=new PrintWriter(sw);

e.printStackTrace(pw);

String exceptionMessage = sw.toString();

}

(2) jsp页面如何打印异常信息

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page isErrorPage="true" import="java.io.*"%>

<%@ taglib uri="http://www.77cn.com.cn/jsp/jstl/core" prefix="c"%>

<%=exception.getMessage()%><br>

<%=exception.getLocalizedMessage()%>

<%

StringWriter sw=new StringWriter();

PrintWriter pw=new PrintWriter(sw);

exception.printStackTrace(pw);

response.setStatus(500);

out.print(sw);

%>

二、C标签if语句的写法

<% if (exception !=null ){%>

<%=exception.getMessage()%><br>

<% }else {%>

error!

<% }%>

三、设置全局错误页面

步骤一、web.xml配置

<error-page>

<error-code>500</error-code> <location>/error.jsp</location> </error-page>

步骤二、编写错误页面

一、如何获取异常信息的String类型信息二、C标签if语句的写法三、设置全局错误页面四、如何为div赋值五、自定义拦截器的使用

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page isErrorPage="true" import="java.io.*"%>

<%@ taglib uri="http://www.77cn.com.cn/jsp/jstl/core" prefix="c"%>

<%=exception.getMessage()%><br>

<%=exception.getLocalizedMessage()%>

<%

StringWriter sw=new StringWriter();

PrintWriter pw=new PrintWriter(sw);

exception.printStackTrace(pw);

response.setStatus(500);

out.print(sw);

%>

四、如何为div赋值

<div id="urlDetailWindow" class="easyui-dialog" title="访问路径详情"

style="width:500px;height:200px;padding:10px" closed="true" shadow="true"

modal="true">

<div id="urlDetailIdDiv"></div>

</div>

function urlDetail(){

} var row = $('#datalist').datagrid('getSelected'); if(row){ $("#urlDetailIdDiv").text(row["url"]); $("#urlDetailWindow").window('open'); }

或者

document.getElementById("urlDetailIdDiv ").innerHTML=" aaaaa ";

五、自定义拦截器的使用

(1)struts.xml配置

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://www.77cn.com.cn/dtds/struts-2.0.dtd">

<struts>

<package name="default" extends="struts-default" namespace="/"> <constant name="struts.ognl.allowStaticMethodAccess" value="true" /> <constant name="struts.multipart.maxSize" value="100000000"/>

一、如何获取异常信息的String类型信息二、C标签if语句的写法三、设置全局错误页面四、如何为div赋值五、自定义拦截器的使用

<interceptors>

<interceptor name="exceptionInterceptor"

class="com.web.interceptor.ExceptionInterceptor">

<param name="includeMethods">execute</param>

</interceptor>

<interceptor name="loginInterceptor"

class="com.web.interceptor.LoginInterceptor"/>

<interceptor-stack name="defaultStack">

<interceptor-ref name="loginInterceptor"/>

<interceptor-ref name="defaultStack"/>

</interceptor-stack>

</interceptors>

<default-interceptor-ref name="defaultStack"></default-interceptor-ref>

<global-results>

<result name="login" type="redirectAction">index</result>

</global-results>

<action name="compile" class="http://www.77cn.com.cnpileAction">

<result name="exec">/WEB-INF/generate/${jspFileName}</result>

<interceptor-ref name="defaultStack"/>

<interceptor-ref name="exceptionInterceptor"/>

</action>

</package>

(2) 编写自定义拦截器类 </struts>

public class ExceptionInterceptor extends MethodFilterInterceptor{

protected Log log = LogFactory.getLog(this.getClass()); /** * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) */ private static final long serialVersionUID = -6071035934377365730L;

@Override

public String doIntercept(ActionInvocation invocation) throws Exception {

String result;

try {

result = invocation.invoke();

一、如何获取异常信息的String类型信息二、C标签if语句的写法三、设置全局错误页面四、如何为div赋值五、自定义拦截器的使用

} catch (Exception e) {

//对捕获到的异常进行处理,例如将 …… 此处隐藏:1863字,全部文档内容请下载后查看。喜欢就下载吧 ……

struts2 拦截器开发的总结.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
    ×
    二维码
    × 游客快捷下载通道(下载后可以自由复制和排版)
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
    × 常见问题(客服时间:周一到周五 9:30-18:00)