詳解Java的Struts架構中棧值和OGNL的使用_java

來源:互聯網
上載者:User

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

值棧可以通過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中的對象被稱為使用井號的符號,但是,值棧中的對象可以被直接引用,例如,如果員工是一個動作類的屬性,那麼就可以得到如下參考:

 <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.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”:

<%@ 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.yiibai.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。如以下畫面:

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

 Size of the valueStack: 3

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.