Strust2的异常处理不同于我们在Java代码中的try{}catch{}语句,而是将异常放在struts.xml配置文件中了,下面我记录下学习Struts2的异常处理的知识点:
Struts2在配置文件中配置的异常主要包括局部异常和全局异常,下面分别说明。
一、配置全局异常:
如下面的struts.xml配置文件所示:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts> <package name="test" extends="struts-default"> <global-results> <result name="usernameException">/usernameException.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="usernameException" exception="com.example.exception.UsernameException"></exception-mapping> </global-exception-mappings> <action name="login" class="com.example.action.LoginAction"> <result name="success">/success.jsp</result> </action> </package> </struts>global-results标签中,定义了一个全局的结果,名为usernameException,对应的JSP页面为usernameException.jsp,该页面内容很简单,只显示”username exception“,下面又定义了一个global-exception-mappings,为全局的异常映射,其中指定了result为上面定义的usernameException,需要注意的是,上面的配置文件中,节点的顺序依次为global-results、global-exception-mappings、action,这个顺序不能乱,不然struts.xml文件会报错
定义好上面的配置文件后,我们要编写与usernameException对应的UsernameException类,该类继承了Exception类,代码如下:
package com.example.exception; public class UsernameException extends Exception { private String msg; public UsernameException(String msg) { this.msg = msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }然后在LoginAction中,执行execute方法时,我们判断如果username为null或空字符串时,则抛出UsernameException,LoginAction的代码如下所示:
package com.example.action; import com.example.exception.UsernameException; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception{ if(username == null || "".equals(username)){ throw new UsernameException("username should not be empty!"); } ActionContext.getContext().getSession().put("username", username); return SUCCESS; } @Override public String toString() { return "LoginAction [username=" + username + ", password=" + password + "]"; } }二、配置局部异常
局部异常跟全局异常的不同之处在于,struts.xml配置文件中的异常信息的配置,放到action标签中了,下面是局部异常的struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts> <package name="test" extends="struts-default"> <action name="login" class="com.example.action.LoginAction"> <exception-mapping result="usernameException" exception="com.example.exception.UsernameException"></exception-mapping> <result name="success">/success.jsp</result> <result name="usernameException">/usernameException.jsp</result> </action> <action name="getBooks" class="com.example.action.GetBooksAction"> <result name="input">/login.jsp</result> <result name="success">/showBooks.jsp</result> </action> </package> </struts>我们在浏览器中访问login.jsp后,username和password都不填,然后提交表单,会发现出现username exception页面,说明Struts2框架已经完成异常的处理,显示了usernameException.jsp页面