`
zhangzuanqian
  • 浏览: 263419 次
  • 来自: ...
社区版块
存档分类
最新评论

struts2 异常处理

阅读更多
在学习struts2的过程中,想到了struts1的错误处理机制,转载一篇 Callan (javaeye)的文章。

利用struts2的异常处理机制可以很方便的实现异常处理,你不再需要在Action中捕获异常,并抛出相关的异常了,这些都交给拦截器来帮你做了。在struts-default.xml中己经设置了拦截器,因此我们可以直接使用声明式异常.struts2-default-xml声明的拦截器如下:



Xml代码
<interceptors> 
<interceptor name="exception"   
class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/> 
</interceptors> 

<interceptors>
<interceptor name="exception"
class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/>
</interceptors>





异常的类型可以分为两种:局部异常映射和全局异常映射



当Action抛出异常时,会在局部中和全局中查找与之相匹配的异常,如果局部和全局中都有满足的异常映射,以局部的为准. 例子说明:



action:



Java代码
public class LogonAction {  
    private String userName;  
    private String userPwd;  
    public String getUserName() {  
        return userName;  
    }  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
    public String getUserPwd() {  
        return userPwd;  
    }  
    public void setUserPwd(String userPwd) {  
        this.userPwd = userPwd;  
    }  
    public String execute() throws Exception{  
        if(userName.equals("callan") && userPwd.equals("fjf")){  
            ActionContext.getContext().getSession().put("userName", "callan");  
            return "succee";  
        } else {  
            throw new SQLException(“用户名密码不正确”);  
        }  
    }  


public class LogonAction {
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String execute() throws Exception{
if(userName.equals("callan") && userPwd.equals("fjf")){
ActionContext.getContext().getSession().put("userName", "callan");
return "succee";
} else {
throw new SQLException(“用户名密码不正确”);
}
}
}

配置文件:

Xml代码
<package name="logon" extends="struts-default"> 
      <!-- global-exception-mappings必需在action的上面 --> 
       <global-exception-mappings> 
            <exception-mapping result="all" exception="java.lang.Exception"> 
                </exception-mapping> 
        </global-exception-mappings> 
        <action name="logon" class="com.LogonAction"> 
            <result name="succee">/logon/welcome.jsp</result> 
            <result name="error">/logon/error.jsp</result> 
            <exception-mapping result="sql" exception="java.sql.SQLException"/></exception-mapping> 
            <exception-mapping result="nullPoint" exception="java.lang.NullPointerException"></exception-mapping> 
            <result name="nullPoint">/logon/nullpoint.jsp</result> 
            <result name="sql">/logon/sql.jsp</result> 
             <result name="all">/logon/all.jsp</result> 
        </action> 
    </package> 

<package name="logon" extends="struts-default">
      <!-- global-exception-mappings必需在action的上面 -->
       <global-exception-mappings>
        <exception-mapping result="all" exception="java.lang.Exception">
                </exception-mapping>
        </global-exception-mappings>
        <action name="logon" class="com.LogonAction">
            <result name="succee">/logon/welcome.jsp</result>
            <result name="error">/logon/error.jsp</result>
            <exception-mapping result="sql" exception="java.sql.SQLException"/></exception-mapping>
            <exception-mapping result="nullPoint" exception="java.lang.NullPointerException"></exception-mapping>
            <result name="nullPoint">/logon/nullpoint.jsp</result>
            <result name="sql">/logon/sql.jsp</result>
             <result name="all">/logon/all.jsp</result>
        </action>
    </package>


上面是个用户登陆的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>
可以输出 用户名密码不正确


分享到:
评论
2 楼 kashu1217 2012-04-13  
請問一下您的代碼為什麼都貼二次?
1 楼 mamingwei 2008-09-11  

相关推荐

Global site tag (gtag.js) - Google Analytics