Struts2 版本 2.2.1
Freemarker版本 2.3.16
此统一处理的目的在于 Web层、Service层抛出的业务异常以统一的格式显示在页面的固定位置。
首先定义我们的业务异常类。
public
abstract
class
BaseException
extends
RuntimeException
{
private static final long serialVersionUID = -6765360320533958383L;
private String messageCode;
public String getMessageCode()
{
return messageCode;
}
public void setMessageCode(String messageCode)
{
this.messageCode = messageCode;
}
public BaseException()
{
super();
}
public BaseException(String message)
{
super(message);
}
public BaseException(String message, Throwable cause)
{
super(message, cause);
}
public BaseException(Throwable cause)
{
super(cause);
}
public BaseException(String messageCode, String message)
{
super(message);
setMessageCode(messageCode);
}
public BaseException(String messageCode, String message, Throwable cause)
{
super(message, cause);
setMessageCode(messageCode);
}
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
private static final long serialVersionUID = -6765360320533958383L;
private String messageCode;
public String getMessageCode()
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
return messageCode;
}
public void setMessageCode(String messageCode)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
this.messageCode = messageCode;
}
public BaseException()
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super();
}
public BaseException(String message)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(message);
}
public BaseException(String message, Throwable cause)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(message, cause);
}
public BaseException(Throwable cause)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(cause);
}
public BaseException(String messageCode, String message)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(message);
setMessageCode(messageCode);
}
public BaseException(String messageCode, String message, Throwable cause)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(message, cause);
setMessageCode(messageCode);
}
public
class
BusinessException
extends
BaseException
{
private static final long serialVersionUID = -1657938434382769721L;
public BusinessException()
{
super();
}
public BusinessException(String message, Throwable cause)
{
super(message, cause);
}
public BusinessException(Throwable cause)
{
super(cause);
}
public BusinessException(String messageCode, String message)
{
super(messageCode, message);
setMessageCode(messageCode);
}
public BusinessException(String messageCode, String message, Throwable cause)
{
super(messageCode, message, cause);
setMessageCode(messageCode);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
private static final long serialVersionUID = -1657938434382769721L;
public BusinessException()
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super();
}
public BusinessException(String message, Throwable cause)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(message, cause);
}
public BusinessException(Throwable cause)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(cause);
}
public BusinessException(String messageCode, String message)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(messageCode, message);
setMessageCode(messageCode);
}
public BusinessException(String messageCode, String message, Throwable cause)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
super(messageCode, message, cause);
setMessageCode(messageCode);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
拦截器类:ErrorHandlingInterceptor.java 用于拦截异常,并在此统一处理
拦截所有的异常,并对其进行处理。
当为 自定义的BusinessException时,根据抛出异常时的msgCode,取得对应的显示信息。
msgCode与显示信息的对应关系 可先配置好,系统启动时将其缓存起来。
如果非BusinessException,则统一显示为 “系统忙,请稍候再试。”
将要显示的信息设置到Request中,下面来看看Freemarker模板的写法:
msg.ftl
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
< script type ="text/javascript" >
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
if (!this.Message)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
this.Message =
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
(function()
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
/**//**
* show client message
*/
Message.showMsg = function(msg)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
document.getElementById("_err_msg_div").innerHTML = msg;
};
})();
};
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
在使用时,只要在页面上想要展现异常信息的地方插入如下代码即可:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
这样 系统中的异常 将会被统一的显示。
当使用js做前台的表单验证的时候,提示用户的输入有问题,则可以使用 Message.showMsg('...'),提示信息也会显示在同一个位置。
这样就实现了异常提示信息的统一展示了。
这是一个比较简易的实现,只提供一个思路。