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

分为:全局异常和局部异常

      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配置文件,配置文件还是比较容易理解的。



评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值