1. 程式人生 > >struts2異常處理,global-results定義全域性結果處理

struts2異常處理,global-results定義全域性結果處理

<global-results>定義全域性結果處理

一般發生異常之後 結果返回errHandler
因為errHandler是由<global-exception-mappings>關聯到Exception這個類了
然後處理結果
 <result name="errHandler" type="chain">
然後它就根據
<param name="actionName">errorProcessor</param>
找action
<action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess"
> <result>/error.jsp</result> </action> 處理了 然後 返回到 error.jsp

在struts.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 <constant name="struts.devMode" value="true" /> 7 <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> 8 <!-- <constant name="struts.custom.i18n.resources" value="itcast"></constant> --> 9 <package
name="struts-global" namespace="/" extends="struts-default"> 10 <global-results> 11 <result name="errHandler" type="chain"> 12 <param name="actionName">errorProcessor</param> 13 </result> 14 </global-results> 15 <global-exception-mappings> 16 <exception-mapping result="errHandler" exception="java.lang.Exception"> 17 </exception-mapping> 18 </global-exception-mappings> 19 20 <action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess"> 21 <result>/error.jsp</result> 22 </action> 23 </package>
複製程式碼

然後其他包都繼承它 就預設使用了其中定義的 錯誤處理

然後實現 類

class="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 }
複製程式碼

例子

struts.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     <constant name="struts.devMode" value="true" />
 7     <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
 8     <!-- <constant name="struts.custom.i18n.resources" value="itcast"></constant> -->
 9     <package name="struts-global" namespace="/" extends="struts-default">
10         <global-results>
11             <result name="errHandler" type="chain">
12                 <param name="actionName">errorProcessor</param>
13             </result>
14         </global-results>
15         <global-exception-mappings>
16             <exception-mapping result="errHandler" exception="java.lang.Exception">
17             </exception-mapping>
18         </global-exception-mappings>
19         
20         <action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess">
21             <result>/error.jsp</result>
22         </action>
23     </package>
24     <include file="struts-user.xml"></include>
25     <include file="struts-login.xml"></include>
26 </struts>
複製程式碼
struts-user.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     </package>
11 </struts>
複製程式碼

然後 如果頁面異常 都會轉向 error.jsp中 顯示

error.jsp可以進行錯誤顯示

因為資訊被放到棧頂了 所以可以取到

<s:property />