struts2:异常处理
Struts2框架提供了自己的异常处理机制,只需要在struts.xml文件中配置异常处理即可,而不需要在Action方法中来捕捉异常。
传统方法
public String execute() throws Exception { try{ //... return SUCCESS; }catch(SQLException ex) { // SQL异常,返回ERROR ex.printStackTrace(); return ERROR; }catch(InvalidInputException ex) { // Invalid异常,返回ERROR ex.printStackTrace(); return ERROR; }catch(exception1 ex) { // 自定义异常 ex.printStackTrace(); return "result1"; } }
Struts2方法
Strut2框架在struts.xml文件中配置异常通常有两种方式:全局异常配置和局部异常配置。分别通过<global-exception-mappings../>标签和<exception-mapping../>标签来配置。
下面模拟一个用户登录的场景,根据不同的输入,抛出不同的异常,然后将由struts2处理。
1. 创建登录页面(login.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>异常测试页面</title> </head> <body> <center> <s:form action="user" theme="xhtml"> <s:textfield label="Name(应该为admin)" name="name"/> <s:textfield label="Age(应该为18)" name="age" /> <s:textfield label="Tel(应该为13800138000)" name="tel"/> <s:submit></s:submit> </s:form> </center> </body> </html>
2. 创建三个异常显示页面
loginException.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>自定义异常SecurityException</title> </head> <body> <h4> <font color="red"><s:property value="exception.message"/></font><br/> <s:property value="exceptionStack"/> </h4> </body> </html>
Exception.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>全局定义异常Excepition</title> </head> <body> <h4> <font color="red"><s:property value="exception.message"/></font><br/> <s:property value="exceptionStack"/> </h4> </body> </html>
SQLException.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>全局定义异常SQLException</title> </head> <body> <h4> <font color="red"><s:property value="exception.message"/></font><br/> <s:property value="exceptionStack"/> </h4> </body> </html>
3. 创建Action类
package com.clzhang.struts2.demo10; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; private String name; private String age; private String tel; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } @Override public String execute() throws Exception { if (!getName().equals("admin")) { throw new SecurityException("用户名错误,应该为admin!"); } else if (!getAge().equals("18")) { throw new Exception("年龄错误,应该为18岁!"); } else if (!getTel().equals("13800138000")) { throw new java.sql.SQLException(); } else { return SUCCESS; } } }
4. 创建自定义异常类
package com.clzhang.struts2.demo10; public class SecurityException extends Exception { private static final long serialVersionUID = 1L; public SecurityException() { super(); } private String message; public SecurityException(String message) { this.message = message; } public String getMessage() { return message; } }
5. 修改struts.xml
加入如下代码:
<global-results> <result name="Exception">/struts2/demo10/Exception.jsp</result> <result name="SQLException">/struts2/demo10/SQLException.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.sql.SQLException" result="SQLException"/> <exception-mapping exception="java.lang.Exception" result="Exception"/> </global-exception-mappings> <action name="user" class="com.clzhang.struts2.demo10.UserAction"> <exception-mapping exception="com.clzhang.struts2.demo10.SecurityException" result="login"/> <result name="login">/struts2/demo10/loginException.jsp </result> <result>/struts2/demo10/success.jsp</result> </action>
6. 启动Tomcat,测试
打开IE,输入地址:http://127.0.0.1:8080/st/struts2/demo10/login.jsp
结果如下:
直接提交(用户名不为admin),则抛出SecurityException,转到loginException.jsp;
输入用户名admin,再次提交,则抛出Exception,转到Exception.jsp;
输入用户名admin,年龄18,再次提交,则抛出SQLException,转到SQLException.jsp;
最后,输全三个输入(admin/18/13800138000),再次提交,转到成功页面!