本文为原创,欢迎转载,转载请注明出处BlogJava。
Struts2 版本 2.2.1
Freemarker版本 2.3.16
此统一处理的目的在于 Web层、Service层抛出的业务异常以统一的格式显示在页面的固定位置。
首先定义我们的业务异常类。

 public abstract class BaseException extends RuntimeException
public abstract class BaseException extends RuntimeException  {
{
 private static final long serialVersionUID = -6765360320533958383L;
    private static final long serialVersionUID = -6765360320533958383L;

 private String messageCode;
    private String messageCode;


 public String getMessageCode()
    public String getMessageCode()  {
{
 return messageCode;
        return messageCode;
 }
    }


 public void setMessageCode(String messageCode)
    public void setMessageCode(String messageCode)  {
{
 this.messageCode = messageCode;
        this.messageCode = messageCode;
 }
    }


 public BaseException()
    public BaseException()  {
{
 super();
        super();
 }
    }


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


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


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


 public BaseException(String messageCode, String message)
    public BaseException(String messageCode, String message)  {
{
 super(message);
        super(message);
 setMessageCode(messageCode);
        setMessageCode(messageCode);

 }
    }


 public BaseException(String messageCode, String message, Throwable cause)
    public BaseException(String messageCode, String message, Throwable cause)  {
{
 super(message, cause);
        super(message, cause);
 setMessageCode(messageCode);
        setMessageCode(messageCode);
 }
    }

 public class BusinessException extends BaseException
public class BusinessException extends BaseException  {
{

 private static final long serialVersionUID = -1657938434382769721L;
    private static final long serialVersionUID = -1657938434382769721L;
 
                   

 public BusinessException()
    public BusinessException()  {
{
 super();
        super();
 }
    }
 
    

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

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

 public BusinessException(String messageCode, String message)
    public BusinessException(String messageCode, String message)  {
{
 super(messageCode, message);
        super(messageCode, message);
 setMessageCode(messageCode);
        setMessageCode(messageCode);
 }
    }
 
    

 public BusinessException(String messageCode, String message, Throwable cause)
    public BusinessException(String messageCode, String message, Throwable cause)  {
{
 super(messageCode, message, cause);
        super(messageCode, message, cause);
 setMessageCode(messageCode);
        setMessageCode(messageCode);
 }
    }
 }
}

拦截器类:ErrorHandlingInterceptor.java 用于拦截异常,并在此统一处理

 public class ErrorHandlingInterceptor extends AbstractInterceptor
public class ErrorHandlingInterceptor extends AbstractInterceptor  {
{

 private static final long serialVersionUID = 1L;
    private static final long serialVersionUID = 1L;

 @Override
    @Override

 public String intercept(ActionInvocation invocation) throws Exception
    public String intercept(ActionInvocation invocation) throws Exception  {
{

 try
        try  {
{
 return invocation.invoke();
            return invocation.invoke();

 } catch (Exception e)
        } catch (Exception e)  {
{
 e.printStackTrace();
            e.printStackTrace();
 handleException(e);
            handleException(e);
 }
        }
 return Action.INPUT;
        return Action.INPUT;
 }
    }
 
    

 /** *//**
    /** *//**
 * 处理异常
     * 处理异常
 * @param e
     * @param e
 */
     */

 private void handleException(Exception e)
    private void handleException(Exception e)  {
{
 boolean handled = false;
        boolean handled = false;
 Throwable throwEx = e;
        Throwable throwEx = e;

 while (throwEx != null)
        while (throwEx != null)  {
{

 if(throwEx instanceof BusinessException)
            if(throwEx instanceof BusinessException)  {
{
 BusinessException be = (BusinessException)throwEx;
                BusinessException be = (BusinessException)throwEx;
 String errorCode = be.getMessageCode();
                String errorCode = be.getMessageCode();
 
                
 // 从缓存中通过ErrorCode取得对应message
                // 从缓存中通过ErrorCode取得对应message
 // 实现略
                // 实现略
 String errorMsg = getMessage(errorCode);
                String errorMsg = getMessage(errorCode);
 
                
 // 页面显示错误提示信息
                // 页面显示错误提示信息
 fillError4Display(errorMsg);
                fillError4Display(errorMsg);
 handled = true;
                handled = true;
 }
            } 
 throwEx = throwEx.getCause();
            throwEx = throwEx.getCause();
 }
        }
 
        

 if(!handled)
        if(!handled)  {
{
 fillDefaultError();
            fillDefaultError();
 }
        }
 }
    }
 
    

 private HttpServletRequest getRequest()
    private HttpServletRequest getRequest()  {
{
 return (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
        return (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
 }
    }
 
    

 private void fillDefaultError()
    private void fillDefaultError()  {
{
 fillError4Display("系统忙,请稍候再试。");
        fillError4Display("系统忙,请稍候再试。");
 }
    }
 
    

 private void fillError4Display(String msg)
    private void fillError4Display(String msg)  {
{
 getRequest().setAttribute("_error_msg_", msg);
        getRequest().setAttribute("_error_msg_", msg);
 }
    }
 }
}
拦截所有的异常,并对其进行处理。
当为 自定义的BusinessException时,根据抛出异常时的msgCode,取得对应的显示信息。
msgCode与显示信息的对应关系 可先配置好,系统启动时将其缓存起来。
如果非BusinessException,则统一显示为 “系统忙,请稍候再试。”
将要显示的信息设置到Request中,下面来看看Freemarker模板的写法:
msg.ftl
 <div id='_err_msg_div'>
<div id='_err_msg_div'>
 <#if Request['_error_msg_']?exists>
    <#if Request['_error_msg_']?exists>
 ${Request['_error_msg_']}
        ${Request['_error_msg_']}
 </#if>
    </#if>
 </div>
</div>


 <script type="text/javascript">
<script type="text/javascript">

 if (!this.Message)
if (!this.Message)  {
{

 this.Message =
    this.Message =  {};
{};

 (function()
    (function()  {
{

 /**//**
        /**//**
 * show client message
         * show client message
 */
         */

 Message.showMsg = function(msg)
        Message.showMsg = function(msg)  {
{
 document.getElementById("_err_msg_div").innerHTML = msg;
            document.getElementById("_err_msg_div").innerHTML = msg;
 };
        };
 })();
    })();
 };
};
 </script>
</script>
在使用时,只要在页面上想要展现异常信息的地方插入如下代码即可:
 <#include "/msg.ftl">
<#include "/msg.ftl">
这样 系统中的异常 将会被统一的显示。
当使用js做前台的表单验证的时候,提示用户的输入有问题,则可以使用 Message.showMsg('...'),提示信息也会显示在同一个位置。
这样就实现了异常提示信息的统一展示了。
这是一个比较简易的实现,只提供一个思路。