Struts2---->声明式异常处理

声明式异常处理

struts2支持声明式异常处理,可以在Action中直接抛出异常而交给struts2来处理,当然需要我们在xml文件中配置,由于抛出同样的异常的处理方法通常都一样,所以如果能在xml中配置全局异常,将会使得开发便捷性大大提高。

一、声明式异常处理(分为action中异常映射和package中的全局异常映射)

以前的异常捕获可能是这样的:

public String update() {

    Article article = new Article();

    article.setContent(content);

    article.setTitle(title);

    article.setId(id);

    try {

        articleService.update(article);

        return SUCCESS;

    } catch (SQLException e) {

        e.printStackTrace();

        return ERROR;

    } catch (InvalidInputException e) {

        e.printStackTrace();

        System.out.println("输入非法");

        return ERROR;

    }

}

采用struts2的声明式异常处理就会简单很多了

1、首先,上面的代码的try catch 就可以全都不要了,但是得新加throw语句抛出异常:

public String update() throws SQLException, InvalidInputException {

    Article article = new Article();

    article.setContent(content);

    article.setTitle(title);

    article.setId(id);

    articleService.update(article);

    return SUCCESS;

}

2、获异常的任务则交给xml配置文件了,

<package name="wow" extends="struts-default">

    <global-results>

        <result name="sql">/Error.jsp</result>

        
    </global-results>

    <global-exception-mappings>

        <exception-mapping result="sql" exception="java.sql.SQLException"></exception-mapping>

        
    </global-exception-mappings>

    <action name="*_*" class="cn.codeplus.action.{2}Action" method="{1}">

        <result name="success">/{1}_{2}_success.jsp</result>

        <result name="error">/{1}_{2}_error.jsp</result>

        <!--<exception-mapping result="sql" exception="java.sql.SQLException"></exception-mapping>-->

    </action>

</package>

用于异常处理的<exception-mapping>标签可以配置在Action中,也可以配置在<global-exception-mappings>,

<exception-mapping result="sql" exception="java.sql.SQLException"></exception-mapping>

<global-exception-mappings>就是全局异常,当然执行Action的时候发生异常时,如果在Action中没有捕获异常而是抛出异常的话,struts2会首先在正在执行的Action中查找<exception-mapping>,寻找对应的Exception进行处理,如果找不到,才会去<global-exception-mappings>去寻找对应的Exception处理,如果还是找不到的话,就只好抛出异常了。

Error.jsp

...

<%@taglib prefix="s" uri="/struts-tags"%>

...

<body>

    <jsp:include page="nav.jsp"></jsp:include>

    <div>

        抱歉,服务器内部错误。

    </div>

    <div>

        <s:property value="exception.message"/>

    </div>

    <s:debug></s:debug>

</body>

...

二、使用继承,共用异常映射

可以在一个package定义全局异常,或者全局结果,其他的包继承该包,实现最少配置。

	<package name="bbs2009_default" extends="struts-default">
		<global-results>
    		<result name="error">/error.jsp</result>
    	</global-results>
    	
		 <global-exception-mappings>
    		<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
    	</global-exception-mappings>
	</package>
	
    <package name="admin" namespace="/admin" extends="bbs2009_default" >
   		

三、实现原理:
Struts2利用exception的拦截器try…catch Action的调用,在catch中做异常的跳转处理。这个拦截器的实现类可以从struts2-core.jar中的struts-default.xml文件中找到: 

struts-default.xml

<struts>

    <package name="struts-default" abstract="true">
        <interceptors>
            <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
       
            <interceptor-stack name="defaultStack">
                <interceptor-ref name="exception"/>
            </interceptor-stack>

       </interceptors>
       
        <default-interceptor-ref name="defaultStack"/>
        
    </package>
</struts>

ExceptionMappingInterceptor.java

public String intercept(ActionInvocation invocation)
{
String result;
    try
    {
            result = invocation.invoke();
    }
    catch(Exception e)
    {
            if(isLogEnabled())
                handleLogging(e);
            List exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();
           String mappedResult = findResultFromExceptions(exceptionMappings, e);
            if(mappedResult != null)
            {
                result = mappedResult;
             publishException(invocation, new ExceptionHolder(e));
            } else
            {
                throw e;
            }
       }
       return result;
}



 

 

posted on 2012-09-01 11:17  小强斋太  阅读(190)  评论(0编辑  收藏  举报

导航