随笔-124  评论-49  文章-56  trackbacks-0

1 自定义异常类 SystemException.java

public class SystemException extends RuntimeException{
 //自定义key
 private String key;
 //自定义参数
 private Object[] values;

 //实现父类的构造方法
 public SystemException() {
  super();
 }

 public SystemException(String message, Throwable cause) {
  super(message, cause);
 }


 public SystemException(String message) {
  super(message);
 }


 public SystemException(Throwable cause) {
  super(cause);
 }

 //自定义构造方法
 public SystemException(String message, String key) {
  super(message);
  this.key=key;
 }

 //自定义构造方法,带一个参数
 public SystemException(String message, String key,Object value) {
  super(message);
  this.key=key;
  this.values=new Object[]{value};
 }
 
 //自定义构造方法,带多个参数
 public SystemException(String message, String key,Object[] values) {
  super(message);
  this.key=key;
  this.values=values;
 }
 
 //相应的get方法
 public String getKey() {
  return key;
 }

 public Object[] getValues() {
  return values;
 }
}
2 自定义异常处理器 SystemExceptionHandler.java

//作用:截获SystemException,并根据SystemException中的信息动态创建ActionMessage等这些错误信息,
        将其存在request中
public class SystemExceptionHandler extends ExceptionHandler{

 /**
  * 处理SystemException异常
  */
 @Override
 public ActionForward execute(Exception ex,//抛出的异常
         ExceptionConfig config,//struts-config.xml中的配置信息
         ActionMapping mapping,
          ActionForm form,
         HttpServletRequest request,
         HttpServletResponse response) throws ServletException {
  
  ActionForward forward=null;
  //创建ActionForward
  if(config.getPath()!=null){
   //有path属性,则根据path创建
   forward=new ActionForward(config.getPath());
  }else {
   //没有path属性,则根据input属性创建
   forward=mapping.getInputForward();
  }
  if(ex instanceof SystemException){
   SystemException se=(SystemException)ex;
   //key可有可无,所以取出key进行判断
   String key=se.getKey();
   ActionMessage error=null;
   //如果自定义的key为空,用struts的
   if(key==null){
    //拿出error.default和message,创建ActionMessage对象
    error=new ActionMessage(config.getKey(),se.getMessage());
   }else {
    //如果自定义的key有值
    if(se.getValues()!=null){
     error=new ActionMessage(key,se.getValues());
    }else {
     //如果自定义的key有值,则根据key创建ActionMessage对象
     error=new ActionMessage(key);
    }
   }
   //将这个ActionMessage放到request中。key为自定义的,error为ActionMessage对象
   //forward是要转到什么地方,根据path属性创建。"request"为scope的一个,也可以
   //用config.getScope()
   this.storeException(request, key, error, forward, config.getScope());
   return forward;
  }
  return super.execute(ex, config, mapping, form, request, response);
 }
}


3 编写配置文件 struts-config.xml

<global-exceptions>
   <exception key="error.default"
        type="java.lang.Exception"
        scope="request"
        path="/common/exception.jsp"
    <!-- 自定义的异常处理类 -->
        handler="org.oa.common.SystemExceptionHandler"/>
</global-exceptions>

4 编写资源文件 ApplicationResources.properties

error.default={0}
error.org.del=Can't Del Orgnation,id is {0}!

5 业务代码

throw new org.oa.common.SystemException("存在子机构,不允许删除!","error.org.del",org.getOname());

posted on 2009-11-30 08:17 junly 阅读(481) 评论(0)  编辑  收藏 所属分类: struts2/struts1.3/JSF

只有注册用户登录后才能发表评论。


网站导航: