struts框架中对异常的处理

struts 框架本身对异常的处理

异常处理类:UsernameException

 

    1. package com.bjsxt.struts;
    2. public class UsernameException extends Exception {
    3.     
    4.     public UsernameException()
    5.     {
    6.         super();
    7.     }
    8.     public UsernameException(String msg)
    9.     {
    10.         super(msg);
    11.     }
    12. }

     

    package
     com.bjsxt.struts;
  1. public class UserManager {
  2.     private static UserManager instance=new UserManager();
  3.     
  4.     UserManager(){}// 这是这个类的构造方法
  5.     
  6.     public static UserManager getInstance()
  7.     {
  8.         return instance;
  9.     }
  10.     public static void login(String username,String password)
  11.     throws UsernameException,PasswordException
  12.     {
  13.         if(!"admin".equals(username))
  14.         {
  15.            throw new UsernameException("用户不能找到");
  16.         }
  17.         if(!"admin".equals(password))
  18.         {
  19.             throw new PassowrdException();
  20.         }
  21.     }
  22. }

struts-config.xml中的配置

  1. <global-exceptions>
  2.              
  3.             <exception key="user.not.found" type="com.bjsxt.struts.UsernameException" path="/error.jsp" handler="org.apache.struts.action.ExceptionHandler"/>
  4.             <exception key="user.password.error" type="com.bjsxt.struts.PasswordException" />
  5.            
  6. </global-exceptions>

下面是正常情况下的个性化处理的异常

  1. package com.bjsxt.struts;
  2. public class UserManager {
  3.     private static UserManager instance=new UserManager();
  4.     
  5.     UserManager(){}// 这是这个类的构造方法
  6.     
  7.     public static UserManager getInstance()
  8.     {
  9.         return instance;
  10.     }
  11.     public static void login(String username,String password)
  12.     throws UsernameException,PasswordException
  13.     {
  14.         if(!"admin".equals(username))
  15.         {
  16.             throw new ErrorCodeException("user.not.found",username);
  17.         }
  18.         if(!"admin".equals(password))
  19.         {
  20.             throw new ErrorCodeException("user.password.error");
  21.         }
  22.     }
  23. }

UsernameError.java类中的构造函数需要带参

  1. package com.bjsxt.struts;
  2. public class UsernameException extends Exception {
  3.     
  4.     public UsernameException()
  5.     {
  6.         super();
  7.     }
  8.     public UsernameException(String msg)
  9.     {
  10.         super(msg);
  11.     }
  12. }

 

ErrorCodeException.java这个异常类的声明

 

  1. package com.bjsxt.struts;
  2. public class ErrorCodeException extends RuntimeException {
  3.     
  4.     private String errorCode;//正常的国际化文件
  5.     
  6.     private Object[] args;//占位符数组
  7.     
  8.     public ErrorCodeException(String errorCode)
  9.     {
  10.         this( errorCode,new Object[]{});//空数组
  11.     }
  12.     
  13.     public ErrorCodeException(String errorCode,String args0)//正常的国际化信息,只有一个字符串的占位符
  14.     {
  15.         this( errorCode,new Object[]{args0} );
  16.     }
  17.     
  18.     public ErrorCodeException(String errorCode,Object[] args)//正常的国际化信息,可以存入占位符的数组
  19.     {
  20.         this.errorCode=errorCode;
  21.         this.args=args;
  22.     }
  23.     public String getErrorCode() {
  24.         return errorCode;
  25.     }
  26.     public Object[] getArgs() {
  27.         return args;
  28.     }
  29. }

个性化异常处理类----具体的实现过程

  1. package com.bjsxt.struts;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.struts.Globals;
  6. import org.apache.struts.action.ActionForm;
  7. import org.apache.struts.action.ActionForward;
  8. import org.apache.struts.action.ActionMapping;
  9. import org.apache.struts.action.ActionMessage;
  10. import org.apache.struts.action.ExceptionHandler;
  11. import org.apache.struts.config.ExceptionConfig;
  12. import org.apache.struts.util.ModuleException;
  13. /**
  14.  * 个性化的异常处理类,是对具体的异常进行处理
  15.  * @author Administrator
  16.  *
  17.  */
  18. public class ErrorCodeExceptionHandler extends ExceptionHandler {
  19.      public ActionForward execute(
  20.                 Exception ex,
  21.                 ExceptionConfig ae,
  22.                 ActionMapping mapping,
  23.                 ActionForm formInstance,
  24.                 HttpServletRequest request,
  25.                 HttpServletResponse response)
  26.                 throws ServletException {
  27.                 ActionForward forward = null;
  28.                 ActionMessage error = null;
  29.                 String property = null;
  30.                 if(!(ex instanceof ErrorCodeException))//如果不是这种异常交给交类处理
  31.                 {
  32.                     return super.execute(ex, ae, mapping, formInstance, request, response);
  33.                 }
  34.                 // Build the forward from the exception mapping if it exists
  35.                 // or from the form input
  36.                 if (ae.getPath() != null) {
  37.                     forward = new ActionForward(ae.getPath());
  38.                 } else {
  39.                     forward = mapping.getInputForward();
  40.                 }
  41.                 // Figure out the error
  42. //              if (ex instanceof ModuleException) {
  43. //                  error = ((ModuleException) ex).getActionMessage();
  44. //                  property = ((ModuleException) ex).getProperty();
  45. //              } else {
  46. //                  error = new ActionMessage(ae.getKey(), ex.getMessage());
  47. //                  property = error.getKey();
  48. //              }
  49.                 
  50.                 //加入个性化的异常处理
  51.                 ErrorCodeException ece=(ErrorCodeException)ex;
  52.                 String errorCode=ece.getErrorCode();
  53.                 Object[] args=ece.getArgs();
  54.                 System.out.println("ErrorCodeExceptionHandler.execute()--errorCode="+errorCode);
  55.                 
  56.                 error = new ActionMessage(errorCode,args);
  57.                 property = error.getKey();
  58.                 this.logException(ex);
  59.                 // Store the exception
  60.                 request.setAttribute(Globals.EXCEPTION_KEY, ex);
  61.                 this.storeException(request, property, error, forward, ae.getScope());
  62.                 return forward;
  63.             }
  64. }

 

struts-config.xml中的配置:

 

  1. <global-exceptions>
  2.              <exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException" path="/error.jsp" handler="com.bjsxt.struts.ErrorCodeExceptionHandler" />
  3.     </global-exceptions>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值