为了使用struts2中的异常处理机制,我们必须打开struts2的异常映射功能,开启异常映射功能需要的一个拦截器,下面的代码片段来自struts-default.xml,在该配置文件中已经开启了struts2的异常映射
<interceptors>
<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"></interceptor>
<interceptor name="permission" class="com.ultratest.oa.interceptor.LoginInterceptor">
</interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="permissionStack">
<interceptor-ref name="exception"></interceptor-ref>
<interceptor-ref name="permission" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="permissionStack"></default-interceptor-ref>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login">/index.jsp</result>
<result name="permissionError">/permissionError.jsp</result>
<result name="flowError">/flowError.jsp</result>
<result name="error">/error.jsp</result>
<result name="oaError">/oaError.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="oaError" exception="com.ultratest.oa.exception.OAException"></exception-mapping>
<exception-mapping result="oaError" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
请注意:golobal-exception-mappings 配置在global-results的后面
在页面oaError页面上使用
异常信息:<s:property value="exception.message"/><br/>
异常栈信息:<s:property value="exceptionStack"/>
使用这两个标签查看异常信息