struts2中错误处理

定义一个 package,然后其他package都继承 这个package    

struts-global

就 有了这个错误处理功能了

 

然后再自己写个类

struts.xml

 1     <constant name="struts.devMode" value="true" />
 2     <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
 3     <!-- <constant name="struts.custom.i18n.resources" value="itcast"></constant> -->
 4     <package name="struts-global" namespace="/" extends="struts-default">
 5         <global-results>
 6             <result name="errHandler" type="chain">
 7                 <param name="actionName">errorProcessor</param>
 8             </result>
 9         </global-results>
10         <global-exception-mappings>
11             <exception-mapping result="errHandler" exception="java.lang.Exception">
12             </exception-mapping>
13         </global-exception-mappings>
14         
15         <action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess">
16         
17             <result>error.jsp</result>
18         </action>
19     </package>
cn.itcast.sh.error.ErrorProcess类
 1 package cn.itcast.sh.error;
 2 
 3 import com.opensymphony.xwork2.ActionContext;
 4 import com.opensymphony.xwork2.ActionSupport;
 5 
 6 public class ErrorProcess extends ActionSupport {
 7     private Exception exception;
 8 
 9     public Exception getException() {
10         return exception;
11     }
12 
13     public void setException(Exception exception) {
14         this.exception = exception;
15     }
16     @Override
17     public String execute()
18     {
19         ActionContext.getContext().getValueStack().push(this.exception.getMessage());
20         return this.SUCCESS;
21     }
22 }

 

 

 

 

其他 strut.xml中

 1 <?xml version="1.0" encoding="utf-8"?>
 2    <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <package name="user" namespace="/" extends="struts-global">
 7         <action name="UserAction_*" method="{1}" class="cn.itcast.sh.action.UserAction">
 8             <result name="userList">/user/list.jsp</result>
 9         </action>
10         
11 
12     </package>
13 </struts>
View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值