struts2声明式异常处理(异常处理: exception-mapping 元素)

来源:互联网 发布:换头像软件faceonbody 编辑:程序博客网 时间:2024/06/07 19:05
 面向对象面向君,不负代码不负卿。 *^o^*

异常处理: 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: 包含着被捕获异常的栈
可以在视图上通过 <s:property> 标签显示异常消息

eg:
struts.xml配置

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"        "http://struts.apache.org/dtds/struts-2.5.dtd"><struts>    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>    <package name="valuestack_1" extends="struts-default">      <global-results>          <result name="input">/index.jsp</result>      </global-results>      <global-exception-mappings>      <exception-mapping exception="java.lang.ArithmeticException" result="input"></exception-mapping>      </global-exception-mappings>        <action name="valuestack" class="com.struts.valuestack.ValueStack" method="execute">            <result name="OK" >/success.jsp</result>        </action>    </package></struts>

Action类:ValueStack

package com.struts.valuestack;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.SessionAware;import java.util.Map;public class ValueStack implements SessionAware,RequestAware {    private  String name ;    private  String price ;    private Map<String,Object> sessionMap ;    private Map<String,Object> requestMap ;    public String getName() {        return name;    }    public String getPrice() {        return price;    }    public void setName(String name) {        this.name = name;    }    public void setPrice(String price) {        this.price = price;    }    public ValueStack(){    }    public String execute(){        requestMap.put("test",new Test()) ;        sessionMap.put("file",this) ;        int i = 10 / 0  ;//此处会发生        return  "OK" ;    }    @Override    public void setRequest(Map<String, Object> map) {        requestMap =  map;    }    @Override    public void setSession(Map<String, Object> map) {        sessionMap  =  map;    }}

index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head>    <title>$Title$</title></head><body><s:debug></s:debug><%--打印出异常--%><s:property value="exception"></s:property><br><%--打印出异常值栈--%><s:property value="exceptionStack"></s:property><form action="valuestack.action" method="post">    <input type="text" name="name"><br>    <input type="text" name="price"><br>    <input type="submit" value="submit"></form></body></html>

显示如图:
输出显示

   大牛,别默默看了。快登陆帮我评论吧! *^o^*
原创粉丝点击