值栈是一个集合中的几个对象保持下列对象提供的顺序:
sn | 对象& 描述 |
---|---|
1 |
temporary objects there are various temporary objects which are created during execution of a page. for example the current iteration value for a collection being looped over in a jsp tag. |
2 |
the model object if you are using model objects in your struts application, the current model object is placed before the action on the value stack |
3 |
the action object this will be the current action object which is being executed. |
4 |
named objects these objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes |
值栈可以通过jsp,velocity或者freemarker的标签。有各种不同的标签在单独的章节中,我们将学习,用于获取和设置struts 2.0 的值栈。 valuestack的对象里面可以得到动作如下:
actioncontext.getcontext().getvaluestack()
一旦拥有了值对象,就可以用下面的方法来操纵该对象:
sn | valuestack的方法及说明 |
---|---|
1 |
object findvalue(string expr) find a value by evaluating the given expression against the stack in the default search order. |
2 |
compoundroot getroot() get the compoundroot which holds the objects pushed onto the stack. |
3 |
object peek() get the object on the top of the stack without changing the stack. |
4 |
object pop() get the object on the top of the stack and remove it from the stack. |
5 |
void push(object o) put this object onto the top of the stack. |
6 |
void set(string key, object o) sets an object on the stack with the given key so it is retrievable by findvalue(key,...) |
7 |
void setdefaulttype(class defaulttype) sets the default type to convert to if no type is provided when getting a value. |
8 |
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. |
9 |
int size() get the number of objects in the stack. |
对象图形导航语言(ognl)是一个功能强大的表达式语言是用来参考值栈上的数据和操纵。 ognl也有助于在数据传输和类型转换。
ognl和jsp表达式语言很相似。 ognl 基础的理念是在 root或默认的对象范围内。默认或根对象的属性,可以参考使用的标记符号(井号)。
如前所述,ognl是基于上下文和struts的构建actioncontext 使用ognl映射。actioncontext中映射包括以下:
application - 应用范围的变量
session - 会话范围的变量
root / value stack - 所有操作变量都保存在这里
request - 请求范围的变量
parameters - 请求参数
atributes - 存储的属性页面,请求,会话和应用范围
重要的是要明白,操作对象是始终可用值栈中的。所以,因此,如果动作对象的属性x和y有随时供使用。
在actioncontext中的对象被称为使用井号的符号,但是,值栈中的对象可以被直接引用,例如,如果员工是一个动作类的属性,那么就可以得到如下参考:
<s:property value="name"/>
来代替
<s:property value="#name"/>
如果会话中有一个属性叫做“login”,可以找回如下:
<s:property value="#session.login"/>
ognl还支持处理的集合 - 即映射,list和set。例如,以显示颜色的下拉列表,可以这样做:
<s:select name="color" list="{'red','yellow','green'}" />
本ognl表达式是巧妙地的解释 "red","yellow","green"为颜色,并此基础上建立一个列表。
ognl表达式将被广泛使用时,在接下来的章节中,我们将研究不同的标签。因此,让我们来看看它使用的一些例子在form标签/标签/数据标签控制和ajax标签。
让我们考虑以下动作类,当我们访问值栈,然后设置几个键,我们将在视图,即访问使用ognl,jsp页面。
package com.h3.struts2; import java.util.*; import com.opensymphony.xwork2.util.valuestack; import com.opensymphony.xwork2.actioncontext; import com.opensymphony.xwork2.actionsupport; public class helloworldaction extends actionsupport{ private string name; public string execute() throws exception { valuestack stack = actioncontext.getcontext().getvaluestack(); map<string, object> context = new hashmap<string, object>(); context.put("key1", new string("this is key1")); context.put("key2", new string("this is key2")); stack.push(context); system.out.println("size of the valuestack: " + stack.size()); return "success"; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
其实,struts 2的值栈的顶部增加了动作时执行。所以,通常的方法是把东西值栈添加 getter/setter方法以使这些值在action类,然后使用<s:property>标签来访问值。以下是展示如何在struts actioncontext 中 valuestack 工作。
让我们创建以下jsp文件 helloworld.jsp 的要 webcontent 文件夹。这个视图将被显示动作返回“success”:
<%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>hello world</title> </head> <body> entered value : <s:property value="name"/><br/> value of key 1 : <s:property value="key1" /><br/> value of key 2 : <s:property value="key2" /> <br/> </body> </html>
我们还需要创建的index.jsp在webcontent文件夹,其内容如下:
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ 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> <title>hello world</title> </head> <body> <h1>hello world from struts2</h1> <form action="hello"> <label for="name">please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="say hello"/> </form> </body> </html>
以下是struts.xml文件的内容:
<?xml version="1.0" encoding="utf-8"?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devmode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.h3.struts2.helloworldaction" method="execute"> <result name="success">/helloworld.jsp</result> </action> </package> </struts>
以下是web.xml文件中的内容:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filterdispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
右键点击项目名称,并单击export > war file创建一个war文件。然后将此war 部署在tomcat 的 webapps目录下。最后,启动tomcat服务器和尝试访问url http://localhost:8080/helloworldstruts2/index.jsp。如以下画面:
现在在给定的文本框中输入任何单词,然后点击"say hello"按钮执行已定义的动作。现在,如果检查生成的日志,会发现下面的文本底部:
size of the valuestack: 3
这将显示以下画面,这将显示任何的值,将进入值为key1和key2,我们已经把它们放入 valuestack。