值棧:
值棧是一個集合中的幾個對象保持下列對象提供的順序:
值棧可以通過JSP,Velocity或者Freemarker的標簽。有各種不同的標簽在單獨的章節中,我們將學習,用于獲取和設置Struts 2.0 的值棧。 ValueStack的對象里面可以得到動作如下:
ActionContext.getContext().getValueStack()
一旦擁有了值對象,就可以用下面的方法來操縱該對象:
OGNL:
對象圖形導航語言(OGNL)是一個功能強大的表達式語言是用來參考值棧上的數據和操縱。 OGNL也有助于在數據傳輸和類型轉換。
OGNL和JSP表達式語言很相似。 OGNL 基礎的理念是在 root或默認的對象范圍內。默認或根對象的屬性,可以參考使用的標記符號(井號)。
如前所述,OGNL是基于上下文和Struts的構建ActionContext 使用OGNL映射。ActionContext中映射包括以下:
application - 應用范圍的變量
session - 會話范圍的變量
root / value stack - 所有操作變量都保存在這里
request - 請求范圍的變量
parameters - 請求參數
atributes - 存儲的屬性頁面,請求,會話和應用范圍
重要的是要明白,操作對象是始終可用值棧中的。所以,因此,如果動作對象的屬性x和y有隨時供使用。
在ActionContext中的對象被稱為使用井號的符號,但是,值棧中的對象可以被直接引用,例如,如果員工是一個動作類的屬性,那么就可以得到如下參考:
1
|
<s:property value= "name" /> |
來代替
1
|
<s:property value= "#name" /> |
如果會話中有一個屬性叫做“login”,可以找回如下:
1
|
<s:property value= "#session.login" /> |
OGNL還支持處理的集合 - 即映射,List和Set。例如,以顯示顏色的下拉列表,可以這樣做:
1
|
<s:select name= "color" list= "{'red','yellow','green'}" /> |
本OGNL表達式是巧妙地的解釋 "red","yellow","green"為顏色,并此基礎上建立一個列表。
OGNL表達式將被廣泛使用時,在接下來的章節中,我們將研究不同的標簽。因此,讓我們來看看它使用的一些例子在Form標簽/標簽/數據標簽控制和Ajax標簽。
ValueStack/OGNL 例子: 創建動作:
讓我們考慮以下動作類,當我們訪問值棧,然后設置幾個鍵,我們將在視圖,即訪問使用OGNL,JSP頁面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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; } } |
其實,Struts 2的值棧的頂部增加了動作時執行。所以,通常的方法是把東西值棧添加 getter/setter方法以使這些值在Action類,然后使用<s:property>標簽來訪問值。以下是展示如何在struts ActionContext 中 ValueStack 工作。
創建視圖
讓我們創建以下JSP文件 helloWorld.jsp 的要 WebContent 文件夾。這個視圖將被顯示動作返回“success”:
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ 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文件夾,其內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ 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文件的內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<? 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.yiibai.struts2.HelloWorldAction" method = "execute" > < result name = "success" >/HelloWorld.jsp</ result > </ action > </ package > </ struts > |
以下是web.xml文件中的內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? 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"按鈕執行已定義的動作。現在,如果檢查生成的日志,會發現下面的文本底部:
1
|
Size of the valueStack: 3 |
這將顯示以下畫面,這將顯示任何的值,將進入值為key1和key2,我們已經把它們放入 ValueStack。