我將Struts 2配置爲將任何java.lang.Exception重定向到記錄異常的特殊Action。我的重定向工作,但我的行動總是得到一個空的異常(即使當我明確地拋出一個異常)。這裏是我的struts.xml文件:將Struts 2異常處理程序映射到Action
<global-results>
<result name="errHandler" type="chain">
<param name="actionName">errorProcessor</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="errHandler" />
</global-exception-mappings>
<action name="errorProcessor" class="myErrorProcessor">
<result name="error">/error.jsp</result>
</action>
<action name="throwExceptions" class="throwExceptions">
<result name="success">done.jsp</result>
</action>
在我的錯誤處理,我有以下幾點:
public class myErrorProcessor extends ActionSupport {
private Exception exception;
public String execute() {
System.out.println("null check: " + (exception == null));
return "error";
}
public void setException(Exception exception) {
this.exception = exception;
}
public Exception getException() {
return exception;
}
}
在throwsException類,我有以下幾點:
public String execute() {
int x = 7/0;
return "success";
}
當我運行我的程序,異常處理程序總是得到一個空異常。我正在使用鏈類型重定向到異常處理程序。我是否需要實現某種ExceptionAware接口? Struts 2異常setter是否調用setException之外的東西?
注意:我在編寫這個程序時試圖關注this tutorial。
我提供了一個重複鏈接教程的行爲,但是它包含全部所需的所有文件以減少錯誤的可能性。 – Quaternion 2012-04-29 18:07:25