局部异常:
<action name="login_*" class="com.cys.action.UserAction" method="{1}">
<result name="ok">/userList.jsp</result>
<result name="logout">/index.jsp</result>
<result name="updateUser">/chainOk.jsp</result>
<!-- Struts2局部异常 exception-mapping 标签的result值与 result标签的name值相对应-->
<!-- 异常捕获 -->
<exception-mapping result="byZero" exception="java.lang.ArithmeticException"></exception-mapping>
<!-- 异常跳转界面 -->
<result name="byZero">/opError.jsp</result>
</action>
全局异常:
<package name="spa" extends="struts-default" namespace="/login">
<!-- Struts2全局异常开始 -->
<!-- 异常跳转界面 -->
<global-results>
<result name="arrayOutIndex">/opError.jsp</result>
<result name="byZero">/opError.jsp</result>
</global-results>
<!-- 异常捕获 -->
<global-exception-mappings>
<exception-mapping result="byZero" exception="java.lang.ArithmeticException"></exception-mapping>
<exception-mapping result="arrayOutIndex" exception="java.lang.ArrayIndexOutOfBoundsException"></exception-mapping>
</global-exception-mappings>
<!-- Struts2全局异常结束 -->
<action name="login_*" class="com.cys.action.UserAction" method="{1}">
<result name="ok">/userList.jsp</result>
<result name="logout">/index.jsp</result>
<result name="updateUser">/chainOk.jsp</result>
<!-- Struts2局部异常 exception-mapping 标签的result值与 result标签的name值相对应-->
<!-- 异常捕获 -->
<exception-mapping result="byZero" exception="java.lang.ArithmeticException"></exception-mapping>
<!-- 异常跳转界面 -->
<result name="byZero">/opError.jsp</result>
</action>
</package>
PS:
全局异常映射的result属性值通常不要使用局部结果,局部异常映射的result属性值可以使用全局结果,也可以使用局部结果
输出异常信息:
//引入Struts2标签库
<%@ taglib prefix="s" uri="/struts-tags" %>
<body>
<p><s:property value="exception.message"/></p>
</body>
web.xml异常配置:
<!-- web.xml配置异常 -->
<error-page>
<!-- 状态吗 -->
<error-code>404</error-code>
<!-- 跳转界面 -->
<location>/error404.jsp</location>
</error-page>
<error-page>
<!-- 异常类型 -->
<exception-type>java.lang.ArithmeticException</exception-type>
<!-- 跳转界面 -->
<location>/errorType.jsp</location>
</error-page>