struts2 ValueStack(值栈)解析

时间:2021-08-16 12:28:20

Struts2一个重要点就是值栈。

ValueStack,是用来存储一些在各个action,或者说是通过s标签、el表达式等给前台Jsp等页面展示的东西。

  ValueStack是一个接口,其内部接口非常简单:

 /*
* Copyright 2002-2007,2009 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opensymphony.xwork2.util; import java.util.Map; /**
* ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated against it. When
* evaluating an expression, the stack will be searched down the stack, from the latest objects pushed in to the
* earliest, looking for a bean with a getter or setter for the given property or a method of the given name (depending
* on the expression being evaluated).
*/
public interface ValueStack { public static final String VALUE_STACK = "com.opensymphony.xwork2.util.ValueStack.ValueStack"; public static final String REPORT_ERRORS_ON_NO_PROP = "com.opensymphony.xwork2.util.ValueStack.ReportErrorsOnNoProp"; /**
* Gets the context for this value stack. The context holds all the information in the value stack and it's surroundings.
*
* @return the context.
*/
public abstract Map<String, Object> getContext(); /**
* Sets the default type to convert to if no type is provided when getting a value.
*
* @param defaultType the new default type
*/
public abstract void setDefaultType(Class defaultType); /**
* Set a override map containing <code>key -> values</code> that takes precedent when doing find operations on the ValueStack.
* <p/>
* See the unit test for ValueStackTest for examples.
*
* @param overrides overrides map.
*/
public abstract void setExprOverrides(Map<Object, Object> overrides); /**
* Gets the override map if anyone exists.
*
* @return the override map, <tt>null</tt> if not set.
*/
public abstract Map<Object, Object> getExprOverrides(); /**
* Get the CompoundRoot which holds the objects pushed onto the stack
*
* @return the root
*/
public abstract CompoundRoot getRoot(); /**
* Attempts to set a property on a bean in the stack with the given expression using the default search order.
*
* @param expr the expression defining the path to the property to be set.
* @param value the value to be set into the named property
*/
public abstract void setValue(String expr, Object value); /**
* Attempts to set a property on a bean in the stack with the given expression using the default search order.
* N.B.: unlike #setValue(String,Object) it doesn't allow eval expression.
* @param expr the expression defining the path to the property to be set.
* @param value the value to be set into the named property
*/
void setParameter(String expr, Object value); /**
* Attempts to set a property on a bean in the stack with the given expression using the default search order.
*
* @param expr the expression defining the path to the property to be set.
* @param value the value to be set into the named property
* @param throwExceptionOnFailure a flag to tell whether an exception should be thrown if there is no property with
* the given name.
*/
public abstract void setValue(String expr, Object value, boolean throwExceptionOnFailure); public abstract String findString(String expr);
public abstract String findString(String expr, boolean throwExceptionOnFailure); /**
* Find a value by evaluating the given expression against the stack in the default search order.
*
* @param expr the expression giving the path of properties to navigate to find the property value to return
* @return the result of evaluating the expression
*/
public abstract Object findValue(String expr); public abstract Object findValue(String expr, boolean throwExceptionOnFailure); /**
* Find a value by evaluating the given expression against the stack in the default search order.
*
* @param expr the expression giving the path of properties to navigate to find the property value to return
* @param asType the type to convert the return value to
* @return the result of evaluating the expression
*/
public abstract Object findValue(String expr, Class asType);
public abstract Object findValue(String expr, Class asType, boolean throwExceptionOnFailure); /**
* Get the object on the top of the stack <b>without</b> changing the stack.
*
* @return the object on the top.
* @see CompoundRoot#peek()
*/
public abstract Object peek(); /**
* Get the object on the top of the stack and <b>remove</b> it from the stack.
*
* @return the object on the top of the stack
* @see CompoundRoot#pop()
*/
public abstract Object pop(); /**
* Put this object onto the top of the stack
*
* @param o the object to be pushed onto the stack
* @see CompoundRoot#push(Object)
*/
public abstract void push(Object o); /**
* Sets an object on the stack with the given key
* so it is retrievable by {@link #findValue(String)}, {@link #findValue(String, Class)}
*
* @param key the key
* @param o the object
*/
public abstract void set(String key, Object o); /**
* Get the number of objects in the stack
*
* @return the number of objects in the stack
*/
public abstract int size(); }

和一个普通的栈没多大区别。

他的实现类就比较复杂了(其实也不复杂...)

public class OgnlValueStack implements Serializable, ValueStack, ClearableValueStack, MemberAccessValueStack {

这里贴一部分。

现在来说说值栈的具体作用:

当用户发出一个请求,产生一个request,随即产生一个valueStack,然后Action中的setXX()方法,添加到值栈中,然后struts2根据响应的内容跳转到下一个jsp页面中,这个页面可以通过s标签或者el表达式去获取属性值。

<%@ 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>Insert title here</title>
</head>
<body>
<s:iterator value="list" status="li">
<s:property value="name"/>
<s:property value="title"/>
<s:property value="content"/>
<s:property value="lastDate"/>
<form action="reply.action">
<input type="hidden" name="leaveWord_id" value="${id}">
<input type="submit" value="回复">
</form>
</br>
</s:iterator>
</body>
</html>

最后,详细的说一下s标签或者el表达式是如何拿到属性值的(值栈的工作原理):

  

     /**
* Gets the override map if anyone exists.
*
* @return the override map, <tt>null</tt> if not set.
*/
public abstract Map<Object, Object> getExprOverrides(); /**
* Get the CompoundRoot which holds the objects pushed onto the stack
*
* @return the root
*/
public abstract CompoundRoot getRoot();

上面的root是一个真正的栈结构,而Map就是ActionContext.

当s标签获取属性值时,先从root中找,看有没有,然后再从map中找。

另外,值栈的生命周期 = Request的生命周期,ActionContext的生命周期 = session的生命周期

博文为原创,如需转载,请标注原处,谢谢。

struts2 ValueStack(值栈)解析的更多相关文章

  1. struts2&lpar;二&rpar;值栈 threadlocal ogal ui

    值栈(重要)和ognl表达式 1.  只要是一个mvc框架,必须解决数据的存和取的问题 2.  Struts2利用值栈来存数据,所以值栈是一个存储数据的内存结构 3.  把数据存在值栈中,在页面上利用 ...

  2. 【Java EE 学习 36】【struts2】【struts2系统验证】【struts2 ognl值栈】【struts2 ongl标签】【struts2 UI标签】【struts2模型驱动和令牌机制】

    一.struts2系统验证 1.基于struts2系统验证的方式实际上就是通过配置xml文件的方式达到验证的目的. 2.实际上系统校验的方法和手工校验的方法在底层的基本实现是相同的.但是使用系统校验的 ...

  3. Struts 中 ActionContext ctx&period;put&lpar;&rpar;把数据放到ValueStack里之数据传输背后机制:ValueStack&lpar;值栈&rpar;

    1.     数据传输背后机制:ValueStack(值栈) 在这一切的背后,是因为有了ValueStack(值栈)! ValueStack基础:OGNL要了解ValueStack,必须先理解OGNL ...

  4. ValueStack值栈和ActionContext

    Struts2在OGNL之上提供的最大附加特性就是支持值栈(ValueStack),在OGNL上下文中只能有一个根对象,Struts2的值栈则允许存在许多虚拟对象. 一:值栈(ValueStack) ...

  5. Struts2的值栈和OGNL牛逼啊

    Struts2的值栈和OGNL牛逼啊 一 值栈简介: 值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务. 二 ...

  6. valueStack&lpar;值栈&rpar;

    值栈 值栈(ValueStack)就是 OGNL 表达式存取数据的地方.在一个值栈中,封装了一次请求所需要的所有数据. 在使用 Struts2 的项目中,Struts2 会为每个请求创建一个新的值栈, ...

  7. &lbrack; SSH框架 &rsqb; Struts2框架学习之三(OGNl和ValueStack值栈学习)

    一.OGNL概述 1.1 什么是OGNL OGNL的全称是对象图导航语言( object-graph Navigation Language),它是一种功能强大的开源表达式语言,使用这种表达式语言,可 ...

  8. Struts2的值栈和对象栈

    ValueStack 如何得到值栈: 如何将对象存入值栈: 让值栈执行表达式来获得值: 在JSP中跳过栈顶元素直接访问第二层: 在JSP中访问值栈对象本身(而不是它们的属性) ActionContex ...

  9. Struts2 的 值栈和ActionContext

    1.ValueStack 和 ActionContext 的关系与区别: -- 相同点:它们都是在一次HTTP请求的范围内使用的,它们的生命周期都是一次请求 -- 不同点:ValueStack 分为对 ...

  10. Struts2 之值栈

    值栈(ValueStack) http://www.cnblogs.com/bgzyy/p/8639893.html 这是我的有关 struts2 的第一篇文章,对于里面我们说到的一个 struts2 ...

随机推荐

  1. AgileEAS&period;NET SOA 中间件平台5&period;2版本下载、配置学习&lpar;四&rpar;:开源的Silverlight运行容器的编译、配置

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  2. tomcat映射路径的配置方法

    一.默认配置 位置:/conf 文件夹里的server.xml文件 <Host appBase="webapps"> appBase:可以指定绝对目录,也可以指定相对于 ...

  3. MS SQL Server带有时间的记录怎样查询

    比如某一张表[A]有一个保存日期包含时间字段[B],如果以这个段[B]作查询条件对数据记录进行查询.也我们得花些心思才能查询到我们想得到的记录. 现在我们需要查询这天2014-06-21的所有记录: ...

  4. c&plus;&plus;模板库&lpar;简介&rpar;

    目 录 STL 简介 ......................................................................................... ...

  5. Andrew Ng机器学习公开课笔记&ndash&semi;Reinforcement Learning and Control

    网易公开课,第16课 notes,12 前面的supervised learning,对于一个指定的x可以明确告诉你,正确的y是什么 但某些sequential decision making问题,比 ...

  6. HW1&period;1

    public class Solution { public static void main(String[] args) { System.out.println("Welcome to ...

  7. C&num;读取Exeal文件

    今天写一个读取Exeal的时候遇到一个问题就是引用了Mircosotf.Office.Interop.Exeal类库的时候没有办法读取到 纠结了好久百度了一下发现别人是这样写的using Exeal= ...

  8. iOS横竖屏

    现在开发的APP大部分界面是竖屏的,只有视频播放的界面和webview阅读文字的界面是可以横屏操作的. 那么就进行如下处理: 1.首先确保APP支持横屏旋转 2.我的App里面都是走UINavigat ...

  9. 设计模式六大原则(4):接口隔离原则(Interface Segregation Principle)

    接口隔离原则: 使用多个专门的接口比使用单一的总接口要好. 一个类对另外一个类的依赖性应当是建立在最小的接口上的. 一个接口代表一个角色,不应当将不同的角色都交给一个接口.没有关系的接口合并在一起,形 ...

  10. 斯坦福大学自然语言处理第一课——引言(Introduction)

    一.课程介绍 斯坦福大学于2012年3月在Coursera启动了在线自然语言处理课程,由NLP领域大牛Dan Jurafsky 和 Chirs Manning教授授课:https://class.co ...