1、编程式异常
* 截获异常
* 创建相应的异常消息
* 传递异常消息
* 转向相应的页面处理异常
2、声明式异常(自动处理的异常)
* 在struts-config.xml文件中配置<exeception/>标签

Code
<action path="/login"
type="com.bjsxt.struts.LoginAction"
name="loginForm"
scope="request"
validate="false"
input="/login.jsp"
>
<!-- type="对应异常类" key对应国际化消息文本,出现异常后,会转到上面的input="/login.jsp",
而path也是指向转向的页面,path比input优先级要高
-->
<exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
<exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login.jsp"/>
</action> * 理解局部和全局exception

Code
<global-exceptions>
<!--
<exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
<exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
<!--
<exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" handler="org.apache.struts.action.ExceptionHandler"/>
<exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" handler="org.apache.struts.action.ExceptionHandler"/>
-->
<!--
<exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException" handler="com.bjsxt.struts.ErrorCodeExceptionHandler"/>
-->
<!--
<exception key="error.exception" type="com.bjsxt.struts.AppException" handler="com.bjsxt.struts.AppExceptionHandler"/>
-->
<exception key="error.exception" type="com.bjsxt.struts.AppException"/>
</global-exceptions> * 注意局部<exception/>标签需要配置到<forward/>标签的前面,详见dtd中的约束
<exeception/>标签中的属性说明:
* key:指异常信息对应的国际化消息文本,这个key值需要在国际化资源文件中定义
* type: 处理那种异常
* path: 定义一但出现异常,需要转向那个页面,如果不定义path,
默认情况下将使用<action>标签中input属性对应的页面
* scope:可以取值request和session,默认为request
* handler:异常的处理类,struts默认采用org.apache.struts.action.ExceptionHandler,
如果做个性化的异常处理可以继承此类覆写相应的方法
参见:ErrorCodeExceptionHandler.java和AppExceptionHandler.java
<1>ErrorCodeException.java

Code
package com.bjsxt.struts;
public class ErrorCodeException extends RuntimeException {
private String errorCode;
//用来填充相应的对象参数
private Object[] args;
public ErrorCodeException(String errorCode) {
this(errorCode, null);
}
public ErrorCodeException(String errorCode, Object args0) {
this(errorCode, new Object[]{args0});
}
public ErrorCodeException(String errorCode, Object[] args) {
this.errorCode = errorCode;
this.args = args;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getArgs() {
return args;
}
}
<2> ErrorCodeExceptionHandler.java

Code
package com.bjsxt.struts;
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 org.apache.struts.util.ModuleException;
public class ErrorCodeExceptionHandler extends ExceptionHandler {
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
if (!(ex instanceof ErrorCodeException)) {
return super.execute(ex, ae, mapping, formInstance, request, response);
}
ActionForward forward = null;
ActionMessage error = null;
String property = null;
// Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
ErrorCodeException ece = (ErrorCodeException)ex;
String errorCode = ece.getErrorCode();
Object[] args = ece.getArgs();
error = new ActionMessage(errorCode, args);
property = error.getKey();
//error = new ActionMessage(ae.getKey(), ex.getMessage());
//property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
return forward;
}
}
<3>AppException.java

Code
package com.bjsxt.struts;
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 org.apache.struts.util.ModuleException;
public class AppExceptionHandler extends ExceptionHandler {
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
if (!(ex instanceof AppException)) {
return super.execute(ex, ae, mapping, formInstance, request, response);
}
ActionForward forward = null;
ActionMessage error = null;
String property = null;
// Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
AppException appe = (AppException)ex;
error = new ActionMessage(ae.getKey(), appe.getMessage());
property = error.getKey();
//error = new ActionMessage(ae.getKey(), ex.getMessage());
//property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
return forward;
}
}
* 截获异常
* 创建相应的异常消息
* 传递异常消息
* 转向相应的页面处理异常
2、声明式异常(自动处理的异常)
* 在struts-config.xml文件中配置<exeception/>标签
<action path="/login"
type="com.bjsxt.struts.LoginAction"
name="loginForm"
scope="request"
validate="false"
input="/login.jsp"
>
<!-- type="对应异常类" key对应国际化消息文本,出现异常后,会转到上面的input="/login.jsp",
而path也是指向转向的页面,path比input优先级要高
-->
<exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
<exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login.jsp"/>
</action>
<global-exceptions>
<!--
<exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
<exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
<!--
<exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" handler="org.apache.struts.action.ExceptionHandler"/>
<exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" handler="org.apache.struts.action.ExceptionHandler"/>
-->
<!--
<exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException" handler="com.bjsxt.struts.ErrorCodeExceptionHandler"/>
-->
<!--
<exception key="error.exception" type="com.bjsxt.struts.AppException" handler="com.bjsxt.struts.AppExceptionHandler"/>
-->
<exception key="error.exception" type="com.bjsxt.struts.AppException"/>
</global-exceptions>
<exeception/>标签中的属性说明:
* key:指异常信息对应的国际化消息文本,这个key值需要在国际化资源文件中定义
* type: 处理那种异常
* path: 定义一但出现异常,需要转向那个页面,如果不定义path,
默认情况下将使用<action>标签中input属性对应的页面
* scope:可以取值request和session,默认为request
* handler:异常的处理类,struts默认采用org.apache.struts.action.ExceptionHandler,
如果做个性化的异常处理可以继承此类覆写相应的方法
参见:ErrorCodeExceptionHandler.java和AppExceptionHandler.java
<1>ErrorCodeException.java
package com.bjsxt.struts;
public class ErrorCodeException extends RuntimeException {
private String errorCode;
//用来填充相应的对象参数
private Object[] args;
public ErrorCodeException(String errorCode) {
this(errorCode, null);
}
public ErrorCodeException(String errorCode, Object args0) {
this(errorCode, new Object[]{args0});
}
public ErrorCodeException(String errorCode, Object[] args) {
this.errorCode = errorCode;
this.args = args;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getArgs() {
return args;
}
}
package com.bjsxt.struts;
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 org.apache.struts.util.ModuleException;
public class ErrorCodeExceptionHandler extends ExceptionHandler {
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
if (!(ex instanceof ErrorCodeException)) {
return super.execute(ex, ae, mapping, formInstance, request, response);
}
ActionForward forward = null;
ActionMessage error = null;
String property = null;
// Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
ErrorCodeException ece = (ErrorCodeException)ex;
String errorCode = ece.getErrorCode();
Object[] args = ece.getArgs();
error = new ActionMessage(errorCode, args);
property = error.getKey();
//error = new ActionMessage(ae.getKey(), ex.getMessage());
//property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
return forward;
}
}
package com.bjsxt.struts;
public class AppException extends RuntimeException {
public AppException(String msg) {
super(msg);
}
}
<4>AppExceptionHandler.javapublic class AppException extends RuntimeException {
public AppException(String msg) {
super(msg);
}
}
package com.bjsxt.struts;
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 org.apache.struts.util.ModuleException;
public class AppExceptionHandler extends ExceptionHandler {
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
if (!(ex instanceof AppException)) {
return super.execute(ex, ae, mapping, formInstance, request, response);
}
ActionForward forward = null;
ActionMessage error = null;
String property = null;
// Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
AppException appe = (AppException)ex;
error = new ActionMessage(ae.getKey(), appe.getMessage());
property = error.getKey();
//error = new ActionMessage(ae.getKey(), ex.getMessage());
//property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
return forward;
}
}
浙公网安备 33010602011771号