Struts2的异常处理机制:
任何成熟的MVC框架都应该提供成就的异常处理机制。Strut2也不例外。Struts2提供了一种声明式的异常处理方式。Struts2也是通过配置的拦截器来实现异常处理机制的。
Struts2的异常处理机制通过在struts.xml文件中配置﹤exception-mapping …﹥元素完成的,配置该元素时,需要指定两个属性:
exception:此属性指定该异常映射所设置的异常类型。
result:此属性指定Action出现该异常时,系统转入result属性所指向的结果。
异常映射也分为两种:
局部异常映射:﹤exception-mapping…﹥元素作为﹤action…﹥元素的子元素配置。
全局异常映射:﹤exception-mapping…﹥元素作为﹤global-exception-mappings﹥元素的子元素配置。
输出异常信息:
使用Struts2的标签来输出异常信息:
﹤s:property value="exception.message"/﹥:输出异常对象本身。
﹤s:property value="exceptionStack"/﹥: 输出异常堆栈信息。
利用struts2的异常处理机制和拦截器机制可以很方便的实现异常处理功能,你不再需要在Action中捕获异常,并抛出相关的异常了,这些都交给拦截器来帮你做了。
1. 在 struts.xml 文件中,声明全局异常映射,以及对应的全局异常转发如下所示:<interceptors>
<interceptor name="exceptionManager" class="com.ufinity.lansin.admin.web.interceptor.ExceptionInterceptor"/>
<interceptor-stack name="commonInterceptor">
<interceptor-ref name="exception" />
</interceptor-stack>
</interceptors>
<global-results>
<result name="EXCEP_ERROR">/WEB-INF/templates/errorpage/Error.ftl</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="EXCEP_ERROR" exception="com.ufinity.lansin.exceptions.SystemException" />
</global-exception-mappings>
2.SystemException是异常处理类,代码如下所示:
public class SystemException extends RuntimeException {
private static final long serialVersionUID = 1L;
public SystemException(String frdMessage) {
super(createFriendlyErrMsg(frdMessage));
}
public SystemException(Throwable throwable) {
super(throwable);
}
public SystemException(Throwable throwable, String frdMessage)
{
super(throwable);
}
private static String createFriendlyErrMsg(String msgBody) {
String prefixStr = "抱歉,";
String suffixStr = " 请稍后再试或与管理员联系!";
StringBuffer friendlyErrMsg = new StringBuffer("");
friendlyErrMsg.append(prefixStr);
friendlyErrMsg.append(msgBody);
friendlyErrMsg.append(suffixStr);
return friendlyErrMsg.toString();
}
}
3.WEB-INF/templates/errorpage/Error.ftl页面
这个页面很简单:
﹤body﹥
﹤h2﹥
出现异常啦
﹤/h2﹥
﹤hr/﹥
﹤h3 style="color:red"﹥
﹤!-- 获得异常对象 --﹥
${exception.message?default("")}
﹤/h3﹥
﹤br/﹥
﹤!-- 异常堆栈信息(开发人员用) --﹥
﹤div style="display:none;"﹥
${exceptionStack?default("")}
﹤/div﹥
﹤/body﹥
4. 在拦截器中,捕获常见的异常,并以友好异常信息抛出,相关代码如下所示:
public class ExceptionInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String result = "";
try {
result = invocation.invoke();
}
catch (NoRightException ex) {
throw new SystemException("没有权限操作,");
}
catch (DataAccessException ex) {
throw new SystemException("数据库操作失败,");
}
catch (NullPointerException ex) {
throw new SystemException("调用了未经初始化的对象或者是不存在的对象,");
}
catch (IOException ex) {
throw new SystemException("IO异常,");
}
catch (ClassNotFoundException ex) {
throw new SystemException("指定的类不存在,");
}
catch (ArithmeticException ex) {
throw new SystemException("数学运算异常,");
}
catch (ArrayIndexOutOfBoundsException ex) {
throw new SystemException("数组下标越界,");
}
catch (IllegalArgumentException ex) {
throw new SystemException("方法的参数错误,");
}
catch (CacheEngineStartupException ex) {
throw new SystemException("缓存引擎出现异常,");
}
catch (CacheException ex) {
throw new SystemException("缓存存取时出现异常,");
}
catch (ConfigLoadException ex) {
throw new SystemException("配置文件加载异常,");
}
catch (DatabaseException ex) {
throw new SystemException("数据库发生异常,");
}
catch (FileNotFoundException ex) {
throw new SystemException("文件未找到,");
}
catch (IllegalOperationException ex) {
throw new SystemException("您的操作是非法的,");
}
catch (IllegalParameterException ex) {
throw new SystemException("提交的参数非法,");
}
catch (MailException ex) {
throw new SystemException("邮件发送出现异常,");
}
catch (NoObjectExistException ex) {
throw new SystemException("你操作的对象已经不存在,");
}
catch (ObjectExistException ex) {
throw new SystemException("你操作的对象已经存在,");
}
catch (SearchException ex) {
throw new SystemException("搜索过程中发生异常,");
}
catch (SearchInstantiationException ex) {
throw new SystemException("搜索实例生成发生异常,");
}
catch (ServiceException ex) {
throw new SystemException("服务器发生异常,");
}
catch (TemplateNotFoundException ex) {
throw new SystemException("模板未找到,");
}
catch (ClassCastException ex) {
throw new SystemException("类型强制转换错误,");
}
catch (SecurityException ex) {
throw new SystemException("违背安全原则异常,");
}
catch (SQLException ex) {
throw new SystemException("操作数据库异常,");
}
catch (NoSuchMethodError ex) {
throw new SystemException("方法末找到异常,");
}
catch (InternalError ex) {
throw new SystemException("Java虚拟机发生了内部错误,");
}
catch (Exception ex) {
throw new SystemException("程序内部错误,操作失败,");
}
return result;
}
}