Struts1.2自定义全局异常类:

来源:互联网 发布:淘宝卖家采集软件用法 编辑:程序博客网 时间:2024/05/22 19:19

1.       修改struts-config文件,加入全局异常处理标签:

<global-exceptions>

       <exception

           key="errors.del" //异常代码

           type="com.ls.oa.exception.SystemException" //异常类型

           //异常处理类

           handler="com.ls.oa.exception.SystemExceptionHandler"                path="/common/exception.jsp"//异常转向页面

           scope="request"//异常保存范围

       >

       </exception>

</global-exceptions>

2.       MessageResources.properties文件中加入国际化消息:

errors.del = Can not delete! {0} has child org!

3.       编写异常类SystemException

package com.ls.oa.exception;

 

@SuppressWarnings("serial")

public class SystemException extends RuntimeException {

 

    private String key;//异常代码

    private Object[] values;//参数

   

    public Object[] getValue() {

       return values;

    }

 

    public String getKey() {

       return key;

    }

 

    public SystemException() {

       super();

    }

 

    public SystemException(String message, Throwable throwable) {

       super(message, throwable);

    }

   

    public SystemException(String message, Throwable throwable, String key) {

       super(message, throwable);

       this.key = key;

    }

 

    public SystemException(String message) {

       super(message);

    }

   

    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;

    }

 

    public SystemException(Throwable throwable) {

       super(throwable);

    }

   

    public SystemException(Throwable throwable, String key) {

       super(throwable);

       this.key = key;

    }

}

4.       编写异常处理类:SystemExceptionHandler

package com.ls.oa.exception;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

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;

 

public class SystemExceptionHandler extends ExceptionHandler {

 

@Override

public ActionForward execute(Exception ex, ExceptionConfig ae,

               ActionMapping mapping, ActionForm formInstance,

               HttpServletRequest request, HttpServletResponse response)

               throws ServletException {

 

        ActionForward forward = null;

        ActionMessage msg = null;

       

        if(ae.getPath() != null) {

               forward = new ActionForward(ae.getPath());

        }else {

               forward = mapping.getInputForward();

        }

       

        if(ex instanceof SystemException) {

               SystemException se = (SystemException) ex;

              

               String key = se.getKey();

               if(key == null) {

                      msg = new ActionMessage(ae.getKey(),ex.getMessage());

               }

               else {

                      if(se.getValue() != null) {

                             msg = new ActionMessage(key,se.getValue());

                      }

                      else {

                             msg = new ActionMessage(key);

                      }

               }

               this.storeException(request, key, msg, forward, ae.getScope());

               return forward;

        }

        return super.execute(ex, ae, mapping, formInstance, request, response);

}

5.    编写exception.jsp页面,加入如下标签:

<html:errors/>

 

原创粉丝点击