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     
 4     <package name="struts-global" namespace="/" extends="struts-default">
 5         <global-results>
 6             <result name="errHandler" type="chain">
 7                 <param name="actionName">errorProcessorparam>
 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.jspresult>
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.jspresult>
 9         action>
10         
11 
12     package>
13 struts>
View Code

 

转载于:https://www.cnblogs.com/friends-wf/p/3781606.html

你可能感兴趣的:(struts2中错误处理)