首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

struts2配置全局错误拦截

2012-09-04 
struts2配置全局异常拦截把这段配置放到package中就行了....global-resultsresult namecommonErrorPa

struts2配置全局异常拦截
把这段配置放到package中就行了....

<global-results><result name="commonErrorPage">/error.jsp</result></global-results><global-exception-mappings>           <exception-mapping name="commonErrorPage"                             exception="java.lang.Exception"                             result="commonErrorPage"/>       </global-exception-mappings>


其实也可以配置一个拦截器,拦截invocation.invoke()是否有异常,有异常则设置错误信息返回到指定页面。请看下面代码:
1.struts.xml的配置:
<interceptors><interceptor name="exceptionInterceptor" /><interceptor-stack name="myStack">    <interceptor-ref name="defaultStack" />    <interceptor-ref name="exceptionInterceptor" />         </interceptor-stack></interceptors><default-interceptor-ref name="myStack" />

2.其中ExceptionInterceptor在Spring中有配置:
<bean id="ExceptionInterceptor"name="code">/** * 异常拦截器,将异常信息捕获,然后统一在异常信息页面进行显示处理 * @author..... *  */public class ExceptionInterceptor extends AbstractInterceptor{    private static final long serialVersionUID = -973363922210992103L;    public static final String EXCEPTION = "commonErrorPage";    @Override    public String intercept(ActionInvocation invocation) throws Exception    {        try        {            return invocation.invoke();        }        catch (Exception e)        {            ActionContext context = invocation.getInvocationContext();            StringWriter writer = new StringWriter();            e.printStackTrace(new PrintWriter(writer, true));            context.put("tipMessage", e.getMessage());            context.put("tipCourse", writer.toString());            return EXCEPTION;        }    }}

热点排行