Struts2异常处理
Struts2提供了强大的异常处理机制,只需要在struts.xml文件中配置异常处理即可,而不需要在Action中捕获异常。
自定义异常类(继承了Exception)
示例如下:
publicclass SecurityException extends Exception {
private String errorMessage;
public SecurityException() {
super();
}
public SecurityException(String message) {
this .errorMessage=message;
}
public String getErrorMessage() {
returnerrorMessage;
}
}
全局异常
<global-exception-mappings> <exception-mapping result="SQLException" exception="java.sql.SQLException" /> <exception-mapping result="Exception" exception="java.lang.Exception" /> </global-exception-mappings> <global-results> <result name="SQLException">/error/SQLException.jsp</result> <result name="Exception">/error/Exception.jsp</result> </global-results> |
局部异常
<action name="hello"class="com.chen.action.Hello">
<exception-mapping result="SecurityException" exception="com.chen.exception.SecurityException"/>
<result name="success">/sayHao.jsp</result>
<result name="SecurityException">/error/SecurityException.jsp</result>
</action>