Struts2 值棧/OGNL

值棧:

值棧是一個集合中的幾個對象保持下列對象提供的順序:

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也有助於在數據傳輸和類型轉換。

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標籤。

ValueStack/OGNL 例子:

創建動作:

讓我們考慮以下動作類,當我們訪問值棧,然後設置幾個鍵,我們將在視圖,即訪問使用OGNL,JSP頁面。

package com.zaixian.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.zaixian.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。如以下畫面:

Hello World Input

現在在給定的文本框中輸入任何單詞,然後點擊"Say Hello"按鈕執行已定義的動作。現在,如果檢查生成的日誌,會發現下麵的文本底部:


Size of the valueStack: 3

這將顯示以下畫面,這將顯示任何的值,將進入值為key1和key2,我們已經把它們放入 ValueStack。


上一篇: Struts2 結果和結果類型 下一篇: Struts2快速入門