当前位置: 代码迷 >> Web前端 >> struts2配置全局错误拦截
  详细解决方案

struts2配置全局错误拦截

热度:100   发布时间:2012-09-04 14:19:30.0
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" class="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"
	class="com.business.interceptor.ExceptionInterceptor"></bean>

3.自己在实现EsbLogInterceptor这个类中拦截代码:
/**
 * 异常拦截器,将异常信息捕获,然后统一在异常信息页面进行显示处理
 * @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;
        }

    }
}
  相关解决方案