分享

Struts2 异常处理

 CevenCheng 2010-09-25

来源:http://blog.sina.com.cn/s/blog_4b62e4a901008jlo.html
Struts2的异常处理机制:

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

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

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

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


异常映射也分为两种:

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

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

Java代码 
  1. package com.dingxun.exception;     
  2.     
  3. import java.sql.SQLException;     
  4. import com.opensymphony.xwork2.ActionSupport;     
  5.     
  6. public class Struts2Exception extends ActionSupport{     
  7.          
  8.     private static final long serialVersionUID = -87324321702425020L;     
  9.     private String username;     
  10.          
  11.     public String getUsername() {     
  12.         return username;     
  13.     }     
  14.     
  15.     public void setUsername(String username) {     
  16.         this.username = username;     
  17.     }     
  18.     
  19.          
  20.     public String execute() throws Exception{     
  21. //      if("logo".equals(getUsername())){     
  22. //          return SUCCESS;     
  23. //      }else{     
  24. //          throw new SQLException("用户名不正确");     
  25. //      }     
  26.         throw  new java.lang.Exception("系统错误");     
  27.     }     
  28.     
  29. }    

 

 

Java代码 
  1. <?xml version="1.0" encoding="UTF-8" ?>     
  2. <!DOCTYPE struts PUBLIC     
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
  4.     "http://struts./dtds/struts-2.0.dtd">     
  5.     
  6. <struts>     
  7.     <package name="aa" extends="struts-default">     
  8.         <global-exception-mappings>     
  9.         <exception-mapping result="all" exception="java.lang.Exception"></exception-mapping>     
  10.         </global-exception-mappings>     
  11.         <action name="execptionTest" class="com.dingxun.exception.Struts2Exception">     
  12.             <result name="success">/exception/result.jsp</result>     
  13.             <exception-mapping result="sql" exception="java.sql.SQLException"/>     
  14.             <exception-mapping result="null" exception="java.lang.NullPointerException"/>     
  15.             <result name="sql">/exception/sql.jsp</result>     
  16.             <result name="null">/exception/null.jsp</result>     
  17.             <result name="all">/exception/all.jsp</result>     
  18.         </action>     
  19.     </package>     
  20. </struts>    

 

 上面是个用户登陆的action及配置,定义了两个局部异常和一个全局异常
当输入的用户名和密码不为callan和fjf时,会抛出SQLException异常,局部异常配置起作用,会定向到sql.jsp,如果注释局部sql异常,全局异常起作用,定向到all.jsp

 

可以使用Struts2的标签来输出异常信息
<s:property value="exception.message"/>
<s:property value="exceptionStack"/>

例如sql.jsp页面

<body>
   <s:property value="exception.message"/>
 </body>
可以输出 用户名密码不正确

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/thunder_1985/archive/2009/09/14/4550618.aspx

 

 

 

 

   输出异常信息:

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

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

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

 

 

 

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

 

 

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

﹤global-results﹥

﹤result name="error"﹥/admin/error/ErrDisplay.ftl﹤/result﹥

﹤/global-results﹥

﹤global-exception-mappings﹥

﹤exception-mapping result="error" exception="com.orizone.hbmobile.hbcm.struts.BusinessException"﹥﹤/exception-mapping﹥

﹤/global-exception-mappings﹥

 

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

 

public class BusinessException extends RuntimeException

{

 

    private static final long serialVersionUID = 0xc1a865c45ffdc5f9L;

  

    public BusinessException(String frdMessage)

    {

        super(createFriendlyErrMsg(frdMessage));

    

    }

 

    public BusinessException(Throwable throwable)

    {

        super(throwable);

    }

 

    public BusinessException(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();

}

}

 

 

2. /admin/error/ErrDisplay.ftl 页面

这个页面很简单:

﹤body﹥

﹤h2﹥

         出现异常啦

﹤/h2﹥

﹤hr/﹥

   ﹤h3 style="color:red"﹥

   ﹤!-- 获得异常对象 --﹥

  

    ${exception.message?default("")}

    ﹤/h3﹥

    ﹤br/﹥

    ﹤!-- 异常堆栈信息(开发人员用) --﹥

    ﹤div style="display:none;"﹥

       ${exceptionStack?default("")}

    ﹤/div﹥

﹤/body﹥

﹤body﹥

﹤h2﹥

         出现异常啦

﹤/h2﹥

﹤hr/﹥

   ﹤h3 style="color:red"﹥

   ﹤!-- 获得异常对象 --﹥

  

    ${exception.message?default("")}

    ﹤/h3﹥

    ﹤br/﹥

    ﹤!-- 异常堆栈信息(开发人员用) --﹥

    ﹤div style="display:none;"﹥

       ${exceptionStack?default("")}

    ﹤/div﹥

﹤/body﹥

 

3. 在拦截器中,捕获常见的异常,并以友好异常信息抛出,相关代码如下所示:

public String intercept(ActionInvocation invocation) throws Exception

{

before(invocation);

String result = "";

 

try{

result = invocation.invoke();

}catch(DataAccessException ex){

throw new BusinessException("数据库操作失败!");

}catch(NullPointerException ex){

throw new BusinessException("调用了未经初始化的对象或者是不存在的对象!");

}catch(IOException ex){

throw new BusinessException("IO异常!");

}catch(ClassNotFoundException ex){

throw new BusinessException("指定的类不存在!");

}catch(ArithmeticException ex){

throw new BusinessException("数学运算异常!");

}catch(ArrayIndexOutOfBoundsException ex){

throw new BusinessException("数组下标越界!");

}catch(IllegalArgumentException ex){

throw new BusinessException("方法的参数错误!");

}catch(ClassCastException ex){

throw new BusinessException("类型强制转换错误!");

}catch(SecurityException ex){

throw new BusinessException("违背安全原则异常!");

}catch(SQLException ex){

throw new BusinessException("操作数据库异常!");

}catch(NoSuchMethodError ex){

throw new BusinessException("方法末找到异常!");

}catch(InternalError ex){

throw new BusinessException("Java虚拟机发生了内部错误");

}catch(Exception ex){

throw new BusinessException("程序内部错误,操作失败!");

}

 

after(invocation, result);

return result;

}

struts2做异常处理还是比较方便的了

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多