在Struts1.X的版本中加入了對異常的處理,Exception Handling,有了它就不需要我們用try/catch等捕獲異常,一旦出現了我們已經定義的異常那么就會轉到相應得頁面,并且攜帶定制的信息。STRUTS框架提供了默認的異常處理org.apache.struts.action.ExceptionHandler,他的execute()方法負責處理異常。在需要實現自定義處理時重寫方法,可以在配置文件定義由誰來處理Action類中擲出的某種異常。
Struts框架處理異常的流程
struts的控制器負責捕獲各種異常,包括控制器運行中本身拋出的異常,以及調用模型的業務方法時拋出的異常。當struts的控制器捕獲到異常后,在異常處理代碼塊中,創建描述信息的actionmessage對象把它保存在acionmessages(或其子類actionerrors)對象中,然后把ACTIONMESSAGES保存在特定范圍(配置文件中的scope)。然后可以用<html:errors>檢索特定范圍內的actionmessages對象。
下面是一個全局的處理類的例子:
首先介紹<global-exception>元素:
<global-exceptions>//元素可以包含一個或者多個<exception>元素
<exception
className=""http://指定和元素對應的配置類,默認的不用動
bundle=""http://Resource Bundle
key="..." // <message-resources parameter="MessageResources" />中的key
type="java.lang.Exception"http://指定需要處理異常類的名字,必須
handler="com.myHander.SystemExceptionHandler"指定異常處理類默認是ExceptionHandler,
path="/pub_exception.jsp"http://指定當前異常發生的時候轉發的路徑
scope="request"http://指定ActionMessages實例存放的范圍
......
>
</exception>
</global-exceptions>
2,定義自己的異常類:SystemException
package com.ex;
public class SystemException extends RuntimeException {
private String key;//得到本地資源文件key
private Object[] values;
public SystemException() {
super();
}
public SystemException(String message, Throwable arg1) {
super(message, arg1);
}
public SystemException(String message) {
super(message);
}
public SystemException(Throwable message) {
super(message);
}
public SystemException(String key,String message, Throwable arg1) {
super(message, arg1);
this.key = key;
}
public SystemException(String key,Object value) {
super();
this.key = key;
values = new Object[]{value};
}
public SystemException(String key,Object value1,Object value2) {
super();
this.key = key;
values = new Object[]{value1,value2};
}
public SystemException(String key,Object[] values) {
super();
this.key = key;
this.values = values;
}
public String getKey() {
return key;
}
public Object[] getValues() {
return values;
}
}
定義自己的異常類處理類:SystemExceptionHandler,extendExceptionHandler,覆蓋execute方法,處理自己的異常類,其他的異常有父類自己處理
package com.ex;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import com.ex.SystemException;
public class SystemExceptionHandler extends ExceptionHandler {
@Override
public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
ActionForward forward = null;
ActionMessage error = null;
String property = null;
if (ae.getPath() != null) {//配置文件中的path
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();//如果沒有找到path,轉到input配置的路徑
}
this.logException(ex);
//處理自定義的異常類SystemException
if(ex instanceof SystemException){
SystemException se = (SystemException)ex;
//如果只有message,而沒有key
if(se.getKey() == null){
error = new ActionMessage(ae.getKey(), ex.getMessage());
property = ae.getKey();
}else{ //SystemException中有key值
error = new ActionMessage(se.getKey(),se.getValues());
property = se.getKey();
}
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
return forward;
}
return super.execute(ex, ae, mapping, formInstance, request, response);
}
}
struts-config.xml中配置:
<global-exceptions>
<exception
type="java.lang.Exception"
handler="com.ex.SystemExceptionHandler"
path="/pub_exception.jsp"
scope="request"
>
</exception>
</global-exceptions>
3,pub_exception.jsp頁面;
獲得異常:<html:errors/>//當然:引入<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
4,測試:TestHander(業務邏輯類)
package com.ex;
import com.ex.SystemException;
public class TestHander {
public void test(int t){
if(t>2){
throw new SystemException("不能大于2!");
//或者throw new SystemException("testkey",t);調用SystemException中的SystemException(String key,Object value) 構成方法得到資源文件定義的信息,當然 在MessageResources.properties文件里加上testkey= {0} con't greater than 2!當然還要加上 <message-resources parameter="MessageResources" />
}
}
}
5,Action類:調用TestHander的test方法:
(主要代碼) new TestHander().test(3);
6,jsp調用acyion頁面頁面:
<a href="testhander.do">異常</a>
一點擊就會轉向pub_exception.jsp頁面顯示:不能大于2!