SSH进阶(5)——Struts2对异常支持

来源:互联网 发布:淘宝买家秀福利店铺名 编辑:程序博客网 时间:2024/06/10 02:28
分为:全局异常和局部异常

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

    ${exception.message }<br>    ${exceptionStack}<br>

if (!("admin".equals(username) && "admin".equals(password))) {            throw new ApplicationException("用户名称或密码错误");        } 



    


<body>    ${exception.message }<br>    ${exceptionStack}<br></body> 

 
 
全局异常:

<global-results>            <result name="global-error">/global_error.jsp</result>        </global-results>        <global-exception-mappings>            <exception-mapping result="global-error" exception="com.bjpowernode.struts2.ApplicationException"/>        </global-exception-mappings> 


 

局部异常:
<action name="login" class="com.bjpowernode.struts2.LoginAction">            <!-- 局部异常 -->            <!--             <exception-mapping result="error" exception="com.bjpowernode.struts2.ApplicationException"/>             -->            <result>/login_success.jsp</result>             <!--             <result name="error">/login_error.jsp</result>             -->        </action>  



 
<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->    <constant name="struts.configuration.xml.reload" value="true"/>    <!-- 会提供更加友好的提示信息 -->    <constant name="struts.devMode" value="true"/>    <!-- 需要继承struts-default包,这样就拥有的最基本的功能 -->    <package name="struts2" extends="struts-default">  


总结:

      Struts2的捕获异常的任务交给xml配置文件,配置文件还是比较容易理解的。



0 0