鍍金池/ 教程/ Java/ 值棧/OGNL
注釋
主題/模板
驗證
有用的資源
Struts 2 注解類型
實例
攔截器
異常處理
表單標(biāo)簽
結(jié)果類型
值棧/OGNL
Spring 集成
數(shù)據(jù)標(biāo)簽
環(huán)境配置
配置
類型轉(zhuǎn)換
動作
Hibernate 集成
本地化
發(fā)送郵件
Ajax 標(biāo)簽
數(shù)據(jù)庫訪問
體系結(jié)構(gòu)
文件上傳
Tiles 集成
概述
基本的 MVC 架構(gòu)
控制標(biāo)簽

值棧/OGNL

值棧

值棧是一個幾個對象的集合,根據(jù)提供的順序保持下列的對象:

序號 對象及描述
1 Temporary Objects

在頁面執(zhí)行期間,有各種各樣的臨時對象被創(chuàng)建。例如,在一個 JSP 標(biāo)簽中集合中循環(huán)的當(dāng)前迭代值。

2 The Model Object

如果你在struts應(yīng)用程序中使用模型對象,在動作之前當(dāng)前模型對象被放置到值棧中。

3 The Action Object

它是被執(zhí)行的當(dāng)前動作對象。

4 Named Objects

這些對象包括 #application,#session,#request,#attr 和 #parameters,并且適用于相應(yīng)的 servlet 范圍。

值棧可以通過 JSP,Velocity 或者 Freemarker 提供的標(biāo)簽來訪問。我們在單獨章節(jié)學(xué)到的各種各樣的標(biāo)簽被用來獲取和設(shè)置 Struts 2.0 的值棧。你可以在你的動作中得到值棧對象,如下所示:

ActionContext.getContext().getValueStack()

一旦你有了值棧對象,就可以用下面的方法來操作這個對象:

序號 值棧方法及描述
1 Object findValue(String expr)

通過用默認(rèn)的搜索順序?qū)Χ褩S嬎憬o定的表達(dá)式找到一個值。

2 CompoundRoot getRoot()

獲取 CompoundRoot,它保存壓入堆棧中的對象。

3 Object peek()

在不改變棧的情況下,獲取棧頂?shù)膶ο蟆?/p>

4 Object pop()

獲取棧頂?shù)膶ο螅⑶野阉鼜臈m斠瞥?/p>

5 void push(Object o)

把這個對象放進(jìn)棧頂中。

6 void set(String key, Object o)

如果一個對象被 findValue(key,...) 檢索,則在堆棧上使用給定的鍵設(shè)置它。

7 void setDefaultType(Class defaultType)

當(dāng)獲得值時,如果沒有提供任何類型,則設(shè)置默認(rèn)的類型轉(zhuǎn)換。

8 void setValue(String expr, Object value)

試圖根據(jù)默認(rèn)的搜索順序在堆棧上使用給定的表達(dá)式設(shè)置一個 bean 的屬性。

9 int size()

獲取堆棧中對象的數(shù)量。

OGNL

對象圖形導(dǎo)航語言(OGNL)是一個強大的表達(dá)式語言,它是用來在值棧上參考和操作數(shù)據(jù)。OGNL 也有助于數(shù)據(jù)傳輸和類型轉(zhuǎn)換。

OGNL 與 JSP 表達(dá)式語言非常類似。OGNL 是以在上下文中有跟對象或默認(rèn)對象的概念為基礎(chǔ)的。默認(rèn)對象或根對象的屬性可以使用的標(biāo)記符號英鎊來引用。

正如前面所提到的,OGNL 是基于上下文的,而 Struts 使用 OGNL 來構(gòu)建 ActionContext 映射。ActionContext 映射由以下組成:

  • application - 應(yīng)用范圍的變量

  • session - 會話范圍的變量

  • root / value stack - 所有的動作變量都保存在這里

  • request - 請求范圍的變量

  • parameters - 請求參數(shù)

  • atributes – 在頁面,請求,會話和應(yīng)用范圍內(nèi)存儲屬性

理解這一點很重要,即動作對象在值棧中始終是可用的。所以,因此如果你的動作對象有 x 和 y 屬性,那么它們是隨時可供你使用的。

ActionContext 中的對象使用英鎊符號來引用,然而,值棧中的對象可以被直接引用,例如如果 employee 是一個動作類的屬性,那么它就可以被引用,如下所示:

<s:property value="name"/>

代替

<s:property value="#name"/>

如果在會話中有一個名為 “l(fā)ogin” 的屬性,你就可以檢索它,如下所示:

<s:property value="#session.login"/>

OGNL 還支持處理集合,即 Map,List 和 Set。例如,為了顯示顏色的下拉列表,你可以這樣做:

<s:select name="color" list="{'red','yellow','green'}" />

OGNL 表達(dá)式巧妙地解釋 “red”,“yellow”,“green” 為顏色,并且在它的基礎(chǔ)上建立一個列表。

當(dāng)我們學(xué)習(xí)不同的標(biāo)簽時,OGNL 表達(dá)式將被廣泛使用在接下來的章節(jié)中。因此,讓我們在 Form 標(biāo)簽 / 控制標(biāo)簽 / 數(shù)據(jù)標(biāo)簽和 Ajax 標(biāo)簽中使用一些例子來看它,而不是孤立地看他們。

值棧/OGNL例子

創(chuàng)建動作

讓我們考慮我們訪問值棧的下面的動作類,然后設(shè)置在視圖即 JSP 頁面上使用 OGNL 訪問的幾個鍵。

package com.tutorialspoint.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;
   }
}

實際上,當(dāng)動作執(zhí)行時,Struts 2 將它添加到值棧的頂部。所以,把東西放在值棧中通常的方法是為你的動作類的值添加 getter/setter 方法,然后使用 標(biāo)簽來訪問值。但是,我將給你展示在 struts 中 ActionContext 和 ValueStack 如何正確地工作。

創(chuàng)建視圖

讓我們在 eclipse 項目的 WebContent 文件夾下創(chuàng)建下面的 jsp 文件 HelloWorld.jsp。在動作返回 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>

我們還需要在 WebContent 文件夾下創(chuàng)建的 index.jsp,其內(nèi)容如下所示:

<%@ 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 文件的內(nèi)容:

<?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.tutorialspoint.struts2.HelloWorldAction" 
         method="execute">
         <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

下面是 web.xml 文件的內(nèi)容:

<?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 來創(chuàng)建一個 War 文件。然后在 Tomcat 的 webapps 目錄下部署這個 WAR。最后,啟動 Tomcat 服務(wù)器和嘗試訪問 URL http://localhost:8080/HelloWorldStruts2/index.jsp. 將會給出下面的畫面:

http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts4.jpg" alt="" />

現(xiàn)在,在給定的文本框中輸入任何單詞,并且單擊 “Say Hello” 按鈕執(zhí)行已經(jīng)定義的動作?,F(xiàn)在,如果你查看生成的日志,就會在底部發(fā)現(xiàn)下面的文字:

Size of the valueStack: 3

無論你輸入什么值,它都將顯示下面的畫面,我們已經(jīng)把 key1 和 key2 的值放入了值棧中。