值棧是一個(gè)集合中的幾個(gè)對(duì)象保持下列對(duì)象提供的順序:
SN | 對(duì)象& 描述 |
---|---|
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的標(biāo)簽。有各種不同的標(biāo)簽在單獨(dú)的章節(jié)中,我們將學(xué)習(xí),用于獲取和設(shè)置Struts 2.0 的值棧。 ValueStack的對(duì)象里面可以得到動(dòng)作如下:
ActionContext.getContext().getValueStack()
一旦擁有了值對(duì)象,就可以用下面的方法來(lái)操縱該對(duì)象:
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. |
對(duì)象圖形導(dǎo)航語(yǔ)言(OGNL)是一個(gè)功能強(qiáng)大的表達(dá)式語(yǔ)言是用來(lái)參考值棧上的數(shù)據(jù)和操縱。 OGNL也有助于在數(shù)據(jù)傳輸和類型轉(zhuǎn)換。
OGNL和JSP表達(dá)式語(yǔ)言很相似。 OGNL 基礎(chǔ)的理念是在 root或默認(rèn)的對(duì)象范圍內(nèi)。默認(rèn)或根對(duì)象的屬性,可以參考使用的標(biāo)記符號(hào)(井號(hào))。
如前所述,OGNL是基于上下文和Struts的構(gòu)建ActionContext 使用OGNL映射。ActionContext中映射包括以下:
application - 應(yīng)用范圍的變量
session - 會(huì)話范圍的變量
root / value stack - 所有操作變量都保存在這里
request - 請(qǐng)求范圍的變量
parameters - 請(qǐng)求參數(shù)
atributes - 存儲(chǔ)的屬性頁(yè)面,請(qǐng)求,會(huì)話和應(yīng)用范圍
重要的是要明白,操作對(duì)象是始終可用值棧中的。所以,因此,如果動(dòng)作對(duì)象的屬性x和y有隨時(shí)供使用。
在ActionContext中的對(duì)象被稱為使用井號(hào)的符號(hào),但是,值棧中的對(duì)象可以被直接引用,例如,如果員工是一個(gè)動(dòng)作類的屬性,那么就可以得到如下參考:
<s:property value="name"/>
來(lái)代替
<s:property value="#name"/>
如果會(huì)話中有一個(gè)屬性叫做“login”,可以找回如下:
<s:property value="#session.login"/>
OGNL還支持處理的集合 - 即映射,List和Set。例如,以顯示顏色的下拉列表,可以這樣做:
<s:select name="color" list="{'red','yellow','green'}" />
本OGNL表達(dá)式是巧妙地的解釋 "red","yellow","green"為顏色,并此基礎(chǔ)上建立一個(gè)列表。
OGNL表達(dá)式將被廣泛使用時(shí),在接下來(lái)的章節(jié)中,我們將研究不同的標(biāo)簽。因此,讓我們來(lái)看看它使用的一些例子在Form標(biāo)簽/標(biāo)簽/數(shù)據(jù)標(biāo)簽控制和Ajax標(biāo)簽。
讓我們考慮以下動(dòng)作類,當(dāng)我們?cè)L問值棧,然后設(shè)置幾個(gè)鍵,我們將在視圖,即訪問使用OGNL,JSP頁(yè)面。
package com.yiibai.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; } }
其實(shí),Struts 2的值棧的頂部增加了動(dòng)作時(shí)執(zhí)行。所以,通常的方法是把東西值棧添加 getter/setter方法以使這些值在Action類,然后使用<s:property>標(biāo)簽來(lái)訪問值。以下是展示如何在struts ActionContext 中 ValueStack 工作。
讓我們創(chuàng)建以下JSP文件 helloWorld.jsp 的要 WebContent 文件夾。這個(gè)視圖將被顯示動(dòng)作返回“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"