ITPub博客

首页 > 应用开发 > Java > struts2的异常处理机制

struts2的异常处理机制

Java 作者:dawn009 时间:2014-06-10 15:26:19 0 删除 编辑

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

一异常处理机制

1、配置

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

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

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

异常映射也分为两种:

局部异常映射:元素作为元素的子元素配置。

全局异常映射:元素作为元素的子元素配置。


2、输出异常信息

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

:输出异常对象本身。

: 输出异常堆栈信息。

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


二、应用实例

1、配置struts.xml

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

           /admin/error/ErrDisplay.ftl

       

      

       

           

              exception="org.basis.common.exception.SystemException">

       


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.getElementByIdx_x("errorMessage").style.display == 'block'){

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

    }else{

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

    }

}

< /script>

< /head>

< body scroll="auto">

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

< tr>

    

       

           

              

           

           

              

           

           

              

           

       

                                        

              

                     

                  查看详细信息

              

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

                  

              

    

< /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文件局部:

           

           

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

           

              

              

           

       

       

 

       

           /common/global/errorPage.jsp

       

      

       

           

              exception="org.basis.common.exception.SystemException">

       

经过这样处理,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;

    }

}

我们知道,做除法的时候,除数不能为零,int i = 1/0;这里应该抛出错误,实际运行一下这个Action,系统提示异常
---------------------->>转载于:http://www.360doc.com/content/13/0503/18/11253639_282730650.shtml#

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29119536/viewspace-1179675/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-08-15

  • 博文量
    595
  • 访问量
    4989246