struts2(exception-mapping 元素 通用标签)

异常处理: exception-mapping 元素

exception-mapping 元素: 配置当前 action 的声明式异常处理
exception-mapping 元素中有 2 个属性
exception: 指定需要捕获的的异常类型。异常的全类名
result: 指定一个响应结果, 该结果将在捕获到指定异常时被执行, 既可以来自当前 action 的声明, 也可以来自 global-results 声明.
可以通过 global-exception-mappings 元素为应用程序提供一个全局性的异常捕获映射. 但在 global-exception-mappings 元素下声明的任何 exception-mapping 元素只能引用在 global-results 元素下声明的某个 result 元素
声明式异常处理机制由 ExceptionMappingInterceptor 拦截器负责处理, 当某个 exception-mapping 元素声明的异常被捕获到时, ExceptionMappingInterceptor 拦截器就会向 ValueStack 中添加两个对象:
exception:
表示被捕获异常的 Exception 对象
exceptionStack: 包含着被捕获异常的栈
可以在视图上通过 <s:property> 标签显示异常消息
例子:
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
   
	<a href="product-save.action?productName=ni">ouhe</a><br>dh
	<a href="ouhesave.action?name=123">ouhe</a><br>

	<form action="product-save.action" >
		
		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"/>
		<br><br>
	
	</form>
  </body>
</html>

product.java

package product;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;

public class Product implements SessionAware{
	
	private Integer productId;
	private String productName;
	private String productDesc;
	private Map<String, Object> session;
	
	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;
	}


	
	public String save(){
       int a=10/0;
		return "details";
	}
	
	public String test(){
		ValueStack a=ActionContext.getContext().getValueStack();
		Product b=new Product();
//		b.setProductName("lalala0");
//		a.push(b);
//		System.out.println("save: " + this);
//		session.put("session",this);
		System.out.println("test");
		return "success";
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.session=arg0;
	}

	
}

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>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.action.extension" value="action,do,"></constant>

<package name="hello" extends="struts-default" >
<global-results><result name="input">/index2.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="product.Product" method="save">
<result name="details">/index1.jsp</result>
</action>

</package>

</struts>

indx2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
   
	<s:debug></s:debug>
<!--	<s:property value="exceptionStack"/><br>-->
	<s:property value="exception"/><br>
	<s:property value="exception.message"/><br>
	<h1>index2</h1>
	
  </body>
</html>

上面是全局的来个不是全局的
在上面代码基础上
做以下改进

<action name="product-save" class="product.Product" method="save">
<exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>
<result name="input">/index2.jsp</result>
<result name="details">/index1.jsp</result>
</action>

通用标签

在这里插入图片描述

*property 标签

property 标签用来输出一个值栈属性的值
在这里插入图片描述

示例:
输出 Action 属性 customerId 的值: <s:property value=“customerId”/>
输出 session 属性 userName 的值:
<s:property value=“#session.userName”/>
如果 value 属性没有给出, ValueStack 值栈栈顶对象的值被输出
在许多情况下, JSP EL 可以提供更简洁的语法

*url 标签

url 标签用来动态地创建一个 URL
在这里插入图片描述

*param 标签

param 标签用来把一个参数传递给包含着它的那个标签
在这里插入图片描述
无论在给出 value 值时有没有使用 %{}, Struts 都会对它进行 ognl 求值
如果想传递一个 String 类型的字符串作为参数值, 必须把它用单引号括起来.
可以把 value 属性的值写在开始标签和结束标签之间. 利用这种方式来传递一个 EL 表达式的值
在上面index1.jsp的基础上
例子:

<s:url value="/testu" action="test" namespace="/a" method="save"var="url">
<s:param  name="id" value="productId"></s:param>对于value值会自动OGNL解析,若不希望在用单引号
</s:url>	
${url}<br>
<s:url  action="test" namespace="/a" method="save" var="url1">
<s:param  name="id" value="productId"></s:param>构建请求action地址
</s:url>	
${url1}<br>
<s:url  value="/ll" includeParams="all" var="url2"> includeParams值 all get
</s:url>	
${url2}<br>

*set 标签

set 标签用来在以下 Map 对象里创建一个键值对:
ValueStack 值栈的 ContextMap 值栈
Map 类型的 session 对象
Map 类型的 application 对象
Map 类型的 request 对象
Map 类型的 page 对象
在这里插入图片描述
例子:
在上面的基础上
index1.jsp

<s:set name="productId" value="productId" scope="request"></s:set>
${requestScope.productId}<br>对于value值会自动OGNL解析

*push 标签

push 标签的功能和 set 标签类似.
push 标签将把一个对象压入 ValueStack 而不是压入 ContextMap.
push 标签在标签起始时把一个对象压入栈, 标签结束时将对象弹出栈.
在这里插入图片描述
例子:
在上面的基础上在index1.jsp
加入

<%Product p=new Product(); 
p.setProductName("nihao");
request.setAttribute("p",p);
%>
<s:push value="#request.p">
${ProductName}</s:push>

*if, else 和 elseif 标签

这三个标签用来进行条件测试, 它们的用途和用法类似于 if, else 和 elseif 关键字. 其中 if 和 elseif 必须有 test 属性
在这里插入图片描述
例子:在上面的基础上
index1.jsp

//可以直接写值栈中的属性
<s:if test="productId>10">//也可 #request.p.age 
 1</s:if>
 <s:elseif test="productId>5">
 2</s:elseif>
<s:else>
3</s:else>

*iterator 标签

iterator 标签用来遍历一个数组, Collection 或一个 Map, 并把这个可遍历对象里的每一个元素依次压入和弹出 ValueStack 栈在这里插入图片描述
在开始执行时, iterator 标签会先把 IteratorStatus 类的一个实例压入 ContextMap, 并在每次遍历循环时更新它. 可以将一个指向 IteratorStatus 对象的变量赋给 status 属性.
例子(导入标签后)
在这里插入图片描述
在product基础上save中相应的改变
在这里插入图片描述
index1.jsp中
在这里插入图片描述
iterator 标签的 status 属性的属性值
在这里插入图片描述
在这里插入图片描述

*sort 标签

sort 标签用来对一个可遍历对象里的元素进行排序.
在这里插入图片描述
例子:
在这里插入图片描述
在这里插入图片描述

*date 标签

date 标签用来对 Date 对象进行排版
在这里插入图片描述
format 属性的值必须是 java.text.SimpleDateFormat 类里定义的日期/时间格式之一.
例子:
在这里插入图片描述

*a 标签

a 标签将呈现为一个 HTML 连接. 这个标签可以接受 HTML 语言中的 a 元素所能接受的所有属性.
在这里插入图片描述
其中强制OGNL解析“%{}”,y因为a标签不自动OGNL解析。

action 标签

action 标签用在页面上来执行一个 action.
action 标签还会把当前 Action 对象压入 ValueStack 值栈的 ContextMap 子栈.
在这里插入图片描述

bean 标签

bean 标签将创建一个 JavaBean, 并把它压入 ValueStack 值栈的 ContextMap 子栈. 这个标签的功能与 JSP 中的 useBean 动作元素很相似
在这里插入图片描述

include 标签

include 标签用来把一个 Servlet 或 JSP 页面的输出包含到当前页面里来.
在这里插入图片描述

append, merge 标签

append 标签用来合并可遍历对象.
merge 标签用来交替合并可遍历对象.
在这里插入图片描述
在这里插入图片描述

generator 标签

generator 标签用来生成一个可遍历对象并把它压入 ValueStack 栈.
generator 标签结束标记将弹出遍历对象

在这里插入图片描述
如果在一个 generator 标签里给出了 converter 属性, 新生成的可遍历对象里的每一个元素都会传递到该属性所指定的方法进行必要的转换.
在这里插入图片描述

subset 标签

subset 标签用来创建一个可遍历集合的子集.
subset 标签通过 decider 属性来创建一个可遍历集合的子集.
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/feiqipengcheng/article/details/106493361