Struts 2 - 值棧/OGNL



值棧

值棧是一組多個物件,按照以下順序儲存物件:

序號 物件和描述
1

臨時物件

在頁面執行期間建立各種臨時物件。例如,在JSP標籤中迴圈遍歷集合的當前迭代值。

2

模型物件

如果在Struts應用程式中使用模型物件,則當前模型物件將放置在值棧上的Action之前。

3

Action物件

這是當前正在執行的Action物件。

4

命名物件

這些物件包括#application、#session、#request、#attr和#parameters,並指向相應的servlet作用域。

值棧可以透過為JSP、Velocity或Freemarker提供的標籤訪問。我們將分別在不同章節中學習各種標籤,這些標籤用於獲取和設定Struts 2.0值棧。您可以在Action中按如下方式獲取ValueStack物件:

ActionContext.getContext().getValueStack()

獲得ValueStack物件後,可以使用以下方法操作該物件:

序號 ValueStack方法和描述
1

Object findValue(String expr)

透過根據預設搜尋順序對棧進行求值給定的表示式來查詢值。

2

CompoundRoot getRoot()

獲取儲存推送到棧上的物件的CompoundRoot。

3

Object peek()

獲取棧頂的物件,而不更改棧。

4

Object pop()

獲取棧頂的物件並將其從棧中移除。

5void push(Object o)

將此物件放入棧頂。

6

void set(String key, Object o)

使用給定的鍵在棧上設定一個物件,以便可以透過findValue(key,…)檢索它。

7

void setDefaultType(Class defaultType)

在獲取值時,如果沒有提供型別,則設定要轉換成的預設型別。

8

void setValue(String expr, Object value)

嘗試使用預設搜尋順序使用給定的表示式在棧中的bean上設定屬性。

9

int size()

獲取棧中物件的個數。

OGNL

物件圖導航語言 (OGNL) 是一種強大的表示式語言,用於引用和操作值棧上的資料。OGNL還有助於資料傳輸和型別轉換。

OGNL與JSP表示式語言非常相似。OGNL基於在上下文中擁有根物件或預設物件的想法。可以使用標記符號(井號)引用預設物件或根物件的屬性。

如前所述,OGNL基於上下文,Struts構建ActionContext對映與OGNL一起使用。ActionContext對映包含以下內容:

  • Application - 應用作用域變數

  • Session - 會話作用域變數

  • Root / 值棧 - 所有Action變數都儲存在此處

  • Request - 請求作用域變數

  • Parameters - 請求引數

  • Attributes - 儲存在頁面、請求、會話和應用程式作用域中的屬性

重要的是要理解,Action物件始終在值棧中可用。因此,如果Action物件具有屬性“x”“y”,則可以隨時使用它們。

ActionContext中的物件使用井號引用,但是值棧中的物件可以直接引用。

例如,如果employee是Action類的屬性,則可以按如下方式引用:

<s:property value = "name"/>

而不是

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

如果會話中有一個名為“login”的屬性,則可以按如下方式檢索它:

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

OGNL還支援處理集合——即Map、List和Set。例如,要顯示顏色下拉列表,您可以執行以下操作:

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

OGNL表示式能夠巧妙地將“red”、“yellow”、“green”解釋為顏色,並基於此構建列表。

在接下來的章節中,學習不同的標籤時,將廣泛使用OGNL表示式。因此,與其單獨檢視它們,不如在表單標籤/控制標籤/資料標籤和Ajax標籤部分中使用一些示例來檢視它們。

值棧/OGNL 示例

建立Action

讓我們考慮以下Action類,我們訪問ValueStack,然後設定一些鍵,我們將在檢視(即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;
   }
}

實際上,Struts 2 在執行時將您的Action新增到值棧的頂部。因此,將內容放入值棧的常用方法是為Action類中的值新增getter/setter,然後使用<s:property>標籤訪問這些值。但我向您展示了ActionContext和ValueStack在Struts中的確切工作方式。

建立檢視

讓我們在eclipse專案的WebContent資料夾中建立下面的jsp檔案HelloWorld.jsp。如果Action返回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資料夾中建立index.jsp,其內容如下:

<%@ 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.tutorialspoint.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>

右鍵單擊專案名稱,然後單擊匯出 > WAR檔案以建立WAR檔案。然後將此WAR部署到Tomcat的webapps目錄中。

最後,啟動Tomcat伺服器並嘗試訪問URL https://:8080/HelloWorldStruts2/index.jsp。這將生成以下螢幕

Hello World Struts 4

現在在給定的文字框中輸入任何單詞,然後單擊“Say Hello”按鈕以執行已定義的Action。現在,如果您檢查生成的日誌,您將在底部找到以下文字:

Size of the valueStack: 3

這將顯示以下螢幕,該螢幕將顯示您輸入的任何值以及我們在ValueStack上放置的key1和key2的值。

廣告
© . All rights reserved.