Struts2异常处理机制

简介: 任何成熟的MVC框架都应该提供成就的异常处理机制,Strut2也不例外。Struts2提供了一种声明式的异常处理方式,Struts2也是通过配置的拦截器来实现异常处理机制的。一、  异常处理机制1. 配置Struts2的异常处理机制是:通过在struts.xml文件中配置﹤exception-mapping …﹥元素完成的,配置该元素时,需要指定两个属性:exception:此属性指定该异

任何成熟的MVC框架都应该提供成就的异常处理机制,Strut2也不例外。Struts2提供了一种声明式的异常处理方式,Struts2也是通过配置的拦截器来实现异常处理机制的。


一、  异常处理机制
1. 配置

Struts2的异常处理机制是:通过在struts.xml文件中配置﹤exception-mapping …﹥元素完成的,配置该元素时,需要指定两个属性:

exception:此属性指定该异常映射所设置的异常类型。

result:此属性指定Action出现该异常时,系统转入result属性所指向的结果。

异常映射也分为两种:

局部异常映射:<exception-mapping…>元素作为<action…>元素的子元素配置。

全局异常映射:<exception-mapping…>元素作为<global-exception-mappings>元素的子元素配置。


2. 输出异常信息

使用Struts2的标签来输出异常信息:

<s:property value="exception.message"/>:输出异常对象本身。

<s:property value="exceptionStack"/>: 输出异常堆栈信息。

利用struts2的异常处理机制和拦截器机制,可以很方便的实现异常处理功能,你不再需要在Action中捕获异常,并抛出相关的异常了,这些都交给拦截器来帮你做了。


二、  应用示例
1. 配置struts.xml文件

1.  在 struts.xml 文件中,声明全局异常映射,以及对应的全局异常转发如下所示:

<global-results>

           <result name="error">/admin/error/ErrDisplay.ftl</result>

       </global-results>

      

       <global-exception-mappings>

           <exception-mapping result="error"

              exception="org.basis.common.exception.SystemException"></exception-mapping>

       </global-exception-mappings>


2. 异常处理类

SystemException是异常处理类,代码如下所示:

package org.basis.common.exception;

 

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. 全局异常处理页面

在系统的/WebRoot/common/global/目录下,新建一个全局的异常处一页面errorPage.jsp。这个页面很简单。

JAVA代码:errorPage.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page isErrorPage="true"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<%

    response.setHeader("Cache-Control","no-cache");

    response.setHeader("Cache-Control","no-store");

    response.setDateHeader("Expires",0);

    response.setHeader("Pragma","no-cache");

%>

<html>

<head>

<script language="javascript">

function showContent(){

    if(document.getElementById("errorMessage").style.display == 'block'){

       document.getElementById("errorMessage").style.display = 'none';

    }else{

       document.getElementById("errorMessage").style.display = 'block';

    }

}

</script>

</head>

<body scroll="auto">

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">

<tr>

    <td align="center" class="bg" valign="top">

       <table width="100%" border="0" cellspacing="0" cellpadding="0" style="table-layout:fixed;word-break:break-all;">

           <tr>

              <td align="center" width="100%" height="80">

                  <img src="${pageContext.request.contextPath}/common/images/basis/systemException.gif" border="0" align="absmiddle">   <s:property value="exception.message" />                   

              </td>

           </tr>

           <tr>

              <td height="30" align="center">

                  <a href="#" onclick="javascript:history.go(-1);"><s:text name="global.return"/></a>&nbsp; &nbsp;

                  <a href="#" onclick="javascript:showContent();">查看详细信息</a>

              </td>

           </tr>

           <tr>

              <td align="left" valign="top">

                  <!-- 异常堆栈信息(开发人员用) -->

                  <div style="display:none;" id="errorMessage">

                  <pre>   <s:property value="exceptionStack" /></pre>

                  </div>

              </td>

           </tr>

       </table>

    </td>

</tr>

</table>

</body>

</html>


4. 自定义异常拦截器类

在这里,应用自定义的异常拦截器,在拦截器中,捕获常见的异常,并以友好异常信息抛出,相关代码如下所示:

JAVA代码:ExceptionInterceptor.java

package org.basis.struts.interceptor;

 

import java.io.IOException;

import java.sql.SQLException;

import org.basis.common.exception.SystemException;

import org.springframework.dao.DataAccessException;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

 

@SuppressWarnings("serial")

public class ExceptionInterceptor extends AbstractInterceptor {

 

    @SuppressWarnings("unchecked")

    public String intercept(ActionInvocation actionInvocation) throws Exception {

       String result = "";

       try {

           result = actionInvocation.invoke();

       } 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 (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;

    }

}


5. 配置异常拦截器

配置这个拦截器,代码如下:

struts.xml文件局部:

<interceptors>

           <interceptor name="checkLogin" class="org.basis.struts.interceptor.CheckLoginInterceptor" />

           <interceptor name="checkException" class="org.basis.struts.interceptor.ExceptionInterceptor" />

           <!-- 定义一个拦截器栈 -->

           <interceptor-stack name="mydefault">

              <interceptor-ref name="defaultStack" />

              <interceptor-ref name="checkException" />

           </interceptor-stack>

       </interceptors>

       <default-interceptor-ref name="mydefault" />

 

       <global-results>

           <result name="error">/common/global/errorPage.jsp</result>

       </global-results>

      

       <global-exception-mappings>

           <exception-mapping result="error"

              exception="org.basis.common.exception.SystemException"></exception-mapping>

       </global-exception-mappings>

经过这样处理,Struts2做异常处理还是比较方便的了。


6. 实际应用效果

下面我们修改一下前面国际华的那个Action,让它抛一个错误。

package demo.struts2.action;

 

import java.util.ArrayList;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

 

public class MessageAction extends ActionSupport {

 

    public String execute() throws Exception {

       // getText(String) string为key

       String str1 = getText("label.helloWorld");

       System.out.println(str1);

       // 带参数的

       String str2 = getText("label.hello", new String[] { "fjf" });

       System.out.println(str2);

 

       // 与上一种实现一样

       List l = new ArrayList();

       l.add("callan");

       String str3 = getText("label.hello", l);

       System.out.println(str3);

      

       String str4 = getText("userName.required");

       System.out.println(str4);

      

       int i = 1/0;

 

       return SUCCESS;

    }

}

目录
相关文章
|
17天前
|
XML 前端开发 Java
SpringMVC 异常处理器
SpringMVC 异常处理器
15 0
|
4月前
SpringMVC异常处理机制
SpringMVC异常处理机制
10 0
|
4月前
SpringMVC之异常处理器
SpringMVC之异常处理器
|
7月前
|
前端开发 测试技术
52SpringMVC - 异常处理器
52SpringMVC - 异常处理器
22 0
springMvc53-简单异常处理
springMvc53-简单异常处理
110 0
springMvc54-简单异常处理二
springMvc54-简单异常处理二
117 0
|
应用服务中间件
SpringMVC的异常处理(超详细)
SpringMVC的异常处理(超详细)
SpringMVC的异常处理(超详细)
|
Java Spring
springMVC异常处理
springMVC异常处理
104 0
|
前端开发 Java 数据安全/隐私保护
SpringMVC(三)——【拦截器、异常处理机制】
SpringMVC(三)——【拦截器、异常处理机制】
SpringMVC(三)——【拦截器、异常处理机制】