struts 框架本身对异常的处理
异常处理类:UsernameException
-
- package com.bjsxt.struts;
- public class UsernameException extends Exception {
- public UsernameException()
- {
- super();
- }
- public UsernameException(String msg)
- {
- super(msg);
- }
- }
- public class UserManager {
- private static UserManager instance=new UserManager();
- UserManager(){}// 这是这个类的构造方法
- public static UserManager getInstance()
- {
- return instance;
- }
- public static void login(String username,String password)
- throws UsernameException,PasswordException
- {
- if(!"admin".equals(username))
- {
- throw new UsernameException("用户不能找到");
- }
- if(!"admin".equals(password))
- {
- throw new PassowrdException();
- }
- }
- }
struts-config.xml中的配置
- <global-exceptions>
- <exception key="user.not.found" type="com.bjsxt.struts.UsernameException" path="/error.jsp" handler="org.apache.struts.action.ExceptionHandler"/>
- <exception key="user.password.error" type="com.bjsxt.struts.PasswordException" />
- </global-exceptions>
下面是正常情况下的个性化处理的异常
- package com.bjsxt.struts;
- public class UserManager {
- private static UserManager instance=new UserManager();
- UserManager(){}// 这是这个类的构造方法
- public static UserManager getInstance()
- {
- return instance;
- }
- public static void login(String username,String password)
- throws UsernameException,PasswordException
- {
- if(!"admin".equals(username))
- {
- throw new ErrorCodeException("user.not.found",username);
- }
- if(!"admin".equals(password))
- {
- throw new ErrorCodeException("user.password.error");
- }
- }
- }
UsernameError.java类中的构造函数需要带参
- package com.bjsxt.struts;
- public class UsernameException extends Exception {
- public UsernameException()
- {
- super();
- }
- public UsernameException(String msg)
- {
- super(msg);
- }
- }
ErrorCodeException.java这个异常类的声明
- package com.bjsxt.struts;
- public class ErrorCodeException extends RuntimeException {
- private String errorCode;//正常的国际化文件
- private Object[] args;//占位符数组
- public ErrorCodeException(String errorCode)
- {
- this( errorCode,new Object[]{});//空数组
- }
- public ErrorCodeException(String errorCode,String 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;
- /**
- * 个性化的异常处理类,是对具体的异常进行处理
- * @author Administrator
- *
- */
- public class ErrorCodeExceptionHandler extends ExceptionHandler {
- 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(!(ex instanceof ErrorCodeException))//如果不是这种异常交给交类处理
- {
- return super.execute(ex, ae, mapping, formInstance, request, response);
- }
- // 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 {
- // error = new ActionMessage(ae.getKey(), ex.getMessage());
- // property = error.getKey();
- // }
- //加入个性化的异常处理
- ErrorCodeException ece=(ErrorCodeException)ex;
- String errorCode=ece.getErrorCode();
- Object[] args=ece.getArgs();
- System.out.println("ErrorCodeExceptionHandler.execute()--errorCode="+errorCode);
- error = new ActionMessage(errorCode,args);
- 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;
- }
- }
struts-config.xml中的配置:
- <global-exceptions>
- <exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException" path="/error.jsp" handler="com.bjsxt.struts.ErrorCodeExceptionHandler" />
- </global-exceptions>