Struts2框架提供了本身的異常處理機制,只須要在struts.xml文件中配置異常處理便可,而不須要在Action方法中來捕捉異常。html
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"; } }
Strut2框架在struts.xml文件中配置異常一般有兩種方式:全局異常配置和局部異常配置。分別經過<global-exception-mappings../>標籤和<exception-mapping../>標籤來配置。java
下面模擬一個用戶登陸的場景,根據不一樣的輸入,拋出不一樣的異常,而後將由struts2處理。sql
<%@ 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>
loginException.jspapp
<%@ 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.jspjsp
<%@ 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>
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; } } }
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; } }
加入以下代碼:ide
<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>
打開IE,輸入地址:http://127.0.0.1:8080/st/struts2/demo10/login.jsp測試
結果以下:ui
直接提交(用戶名不爲admin),則拋出SecurityException,轉到loginException.jsp;this
輸入用戶名admin,再次提交,則拋出Exception,轉到Exception.jsp;
輸入用戶名admin,年齡18,再次提交,則拋出SQLException,轉到SQLException.jsp;
最後,輸全三個輸入(admin/18/13800138000),再次提交,轉到成功頁面!