異常處理:exception-mapping元素
execute-mapping元素: 配置當前action的聲明式異常處理
execute-mapping元素中有2個屬性
--exception:指出需要捕獲的異常類型,異常全類名
--result:指定一個響應結果,該結果將在捕獲到指定異常時被執行,既可以來自當前action的聲明,也可以來自global-result聲明。
<action name="product-save" class="com.wul.strust2.ValueStack.Product"
method="save">
<exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>
<result name="input">/input.jsp</result>
<result>/details.jsp</result>
</action>
可以通過global-exception-mappings元素爲應用程序提出一個全局性的異常捕獲映射。但在global-exception-mappings元素下聲明的
任何exception-mapping元素只能引用在global-result元素下聲明的某個result元素。(注意:global-result必須寫在global-exception-mappings前)
<global-results>
<result name="input">/input.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>
</global-exception-mappings>
聲明式異常處理機制由ExceptionMappingInterceptor攔截器負責處理,當某個exception-mapping元素聲明的異常被捕獲到時,
ExceptionMappingInterceptor攔截器就會向ValueStack中添加兩個對象:
--exception 表示被捕獲異常的Exception對象
--exceptionStack包含着被捕獲異常的棧
可以在視圖上通過<s:property>標籤顯示異常消息
下面給出範例
抓取 10/0 的異常
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 打開靜態方法的調用 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="wul" namespace="/" extends="struts-default">
<global-results>
<result name="input">/input.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>
</global-exception-mappings>
<action name="product-save" class="com.wul.strust2.ValueStack.Product"
method="save">
<!-- <exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>
<result name="input">/input.jsp</result> -->
<result>/details.jsp</result>
</action>
</package>
</struts>
Product.java
package com.wul.strust2.ValueStack;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
public class Product implements RequestAware,SessionAware{
private Integer productId;
private String productName;
private String productDesc;
private double productPrice;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
@Override
public String toString() {
return "Product [productId=" + productId + ", productName="
+ productName + ", productDesc=" + productDesc
+ ", productPrice=" + productPrice + "]";
}
public String save(){
System.out.println("save: "+this.toString());
<span style="color:#ff0000;">int i = 10 / 0;</span>
//1.獲取值棧
ValueStack valueStack = ActionContext.getContext().getValueStack();
//2.創建Test對象,併爲其屬性賦值
Test test = new Test();
test.setProductDesc("wul");
test.setProductName("520");
//3.把Test對象壓入值棧的棧頂
valueStack.push(test);
//ProductPrice: <s:property value="[0].productPrice"/>會讀取“wul”和“520”
sessionMap.put("product", this);
requestMap.put("test", test);
return "success";
}
private Map<String,Object> sessionMap;
@Override
public void setSession(Map<String, Object> sessionMap) {
// TODO 自動生成的方法存根
this.sessionMap = sessionMap;
}
private Map<String,Object> requestMap;
@Override
public void setRequest(Map<String, Object> requestMap) {
// TODO 自動生成的方法存根
this.requestMap = requestMap;
}
}
input.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>Hello</title>
</head>
<body>
<s:debug></s:debug>
<!--
<s:property value="exceptionStack"/>
<br>
-->
<s:property value="exception"/>-
${exception}
<br>
<s:property value="exception.message"/>-
${exception.message}
<br>
<form action="product-save.action" method="post">
ProductName:<input type="text" name="productName"/>
<br><br>
ProductDesc:<input type="text" name="productDesc"/>
<br><br>
ProductPrice:<input type="text" name="productPrice"/>
<br><br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>