18.Struts2__声明式异常处理

来源:互联网 发布:基于arm的ubuntu 编辑:程序博客网 时间:2024/05/29 02:52

异常处理: exception-mapping 元素
exception-mapping 元素: 配置当前 action 的声明式异常处理
exception-mapping 元素中有 2 个属性
exception: 指定需要捕获的的异常类型。异常的全类名
result: 指定一个响应结果, 该结果将在捕获到指定异常时被执行, 既可以来自当前 action 的声明, 也可以来自 global-results 声明.
可以通过 global-exception-mappings 元素为应用程序提供一个全局性的异常捕获映射. 但在 global-exception-mappings 元素下声明的任何 exception-mapping 元素只能引用在 global-results 元素下声明的某个 result 元素
声明式异常处理机制由 ExceptionMappingInterceptor 拦截器负责处理, 当某个 exception-mapping 元素声明的异常被捕获到时, ExceptionMappingInterceptor 拦截器就会向 ValueStack 中添加两个对象:
exception: 表示被捕获异常的 Exception 对象
exceptionStack: 包含着被捕获异常的栈
可以在视图上通过 标签显示异常消息


1.在会出现exception的action中,声明
:exception-mapping,和result

<package name="product" extends="struts-default">        <action name="product_save" class="com.hgh.struts2.common.Product" method="save">            <exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>            <result name="input">/input.jsp</result>            <result>/details.jsp</result>        </action>    </package>

页面查看异常信息:

    <s:debug></s:debug>    错误信息:<s:property value="exception"/>    <br>    错误信息:<s:property value="exception.message"/>    <br>    错误信息:${exception }    <br>    错误信息:${exception.message }
我们一般继承了默认的包:<package name="struts-default" abstract="true">默认的包中有该拦截器<default-interceptor-ref name="defaultStack"/> 拦截器栈中有这个拦截器<interceptor-stack name="defaultStack">                <interceptor-ref name="exception"/><interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
erceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>public class ExceptionMappingInterceptor extends AbstractInterceptor @Override    public String intercept(ActionInvocation invocation) throws Exception {        String result;        try {            result = invocation.invoke();        } catch (Exception e) {            if (isLogEnabled()) {                handleLogging(e);            }            //读取异常配置信息            List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();            ExceptionMappingConfig mappingConfig = this.findMappingFromExceptions(exceptionMappings, e);            if (mappingConfig != null && mappingConfig.getResult()!=null) {                Map parameterMap = mappingConfig.getParams();                // create a mutable HashMap since some interceptors will remove parameters, and parameterMap is immutable                invocation.getInvocationContext().setParameters(new HashMap<String, Object>(parameterMap));                result = mappingConfig.getResult();                publishException(invocation, new ExceptionHolder(e));            } else {                throw e;            }        }//将异常放入栈顶 protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) {        invocation.getStack().push(exceptionHolder);    }
0 0
原创粉丝点击