Struts2_声明式异常处理

异常处理: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>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值