1、exception-mapping:用于当前action的声明式异常处理,就是出错的时候,设置当前action应该有什么反应和表现
2、有两个属性
- result:指定的相应结果,可指定当前actino的result,也可指定全局result
- exception:指定需要捕获的异常类型
3、全局标签:
- global-exception-mappings:全局异常捕获
- global-results:全局结果
- global-exception-mappings下捕获的异常只能引用global-results下的元素
4、输出异常到页面:
- 可以用< s:property value=”异常参数名”>标签,显示异常信息
- exception:表示异常的Exception对象
- exceptionStack:捕获的异常信息
5、sturts.xml例子:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.action.extension" value="action,do,"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="GBexcp">/globalResult.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="GBexcp" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="Test_Exception" class="com.demo.sshtest.ExceptionTest" method="Test_Exception">
<exception-mapping result="excp" exception="java.lang.Exception">
<result name="excp">/Test_Exception.jsp</result>
</action>
</package>
</struts>
6、页面打印异常:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<% request.setCharacterEncoding("UTF-8"); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Exception</title>
</head>
<body>
<br><br>
exception>>>>> <s:property value="exception"/>
<br><br>
exceptionStack>>>>> <s:property value="exceptionStack"/>
<br><br>
<s:debug></s:debug>
</body>
</html>