鍍金池/ 教程/ Java/ Struts2 值棧/OGNL
設(shè)置復(fù)選框的默認(rèn)值
<s:if>, <s:elseIf>, <s:else> 標(biāo)簽示例
下拉框示例
Struts2 &lt;s:bean&gt;標(biāo)簽示例
Struts2 &lt;s:date&gt;標(biāo)簽示例
<s:text>標(biāo)簽示例
Struts2的ActionError&ActionMessage示例
Struts2 MySQL數(shù)據(jù)庫(kù)訪問
Struts2上傳多個(gè)文件例子
Struts2+Log4j集成
Struts 2 hello world (XML版本)
Struts2和Spring集成
Struts2 控件標(biāo)簽
Struts2 和 JSON 實(shí)例
Struts2 環(huán)境配置
Struts2 &lt;s:checkboxlist&gt;多個(gè)復(fù)選框例子
Struts2 注釋類型
在Struts2中配置靜態(tài)參數(shù)
Struts2 驗(yàn)證框架
Struts2 param 標(biāo)簽
Struts2 &lt;s:hidden&gt;隱藏值例子
Struts2下載文件實(shí)例
Struts2 &lt;sx:autocompleter&gt;自動(dòng)提示例子
Struts2 action標(biāo)簽
autocompleter自動(dòng)完成示例
Struts2 merge標(biāo)簽
Struts2 &lt;s:action&gt;標(biāo)簽示例
<s:property>標(biāo)簽示例
Struts2 &lt;s:sort&gt;標(biāo)簽示例
Struts2和Hibernate集成
自動(dòng)選擇下拉框示例
Struts2 結(jié)果和結(jié)果類型
Struts2 是什么?
如何刪除Struts2動(dòng)作的后綴擴(kuò)展名
Struts2 Hello World 實(shí)例
<s:action>標(biāo)簽示例
<s:bean>標(biāo)簽示例
Struts2 + Quartz調(diào)度集成實(shí)例
如何獲取ServletContext對(duì)象
Struts2 &lt;s:i18n&gt;標(biāo)簽示例
Struts2 append標(biāo)簽
i18n 或本地化示例
autocompleter + JSON 示例
Struts2獲取HttpServletRequest實(shí)例
updownselect 示例
Struts2 &lt;s:debug&gt;標(biāo)簽示例
Struts2重寫攔截器參數(shù)
多個(gè)Struts配置文件示例
<s:i18n>標(biāo)簽示例
Struts2中文本地化問題
文件上傳示例
Struts2 &lt;s:property&gt;標(biāo)簽實(shí)例
Struts2攔截器棧的例子
<s:merge>標(biāo)簽示例
Struts2 數(shù)據(jù)標(biāo)簽
Struts2 &lt;s:merge&gt;標(biāo)簽示例
復(fù)選框checkboxes 示例
Struts2 generator標(biāo)簽
如何配置Struts2全局資源包
key 屬性示例
Struts2 注釋
FilterDispatcher 和 StrutsPrepareAndExecuteFilter區(qū)別?
Struts 2 Hello World (注解版本)
Struts 2 + Log4j 集成實(shí)例
Struts2 值棧/OGNL
Struts2配置Action類的靜態(tài)參數(shù)
Struts2 本地化/國(guó)際化(i18n)
Chinese 本地化問題
Struts2單選預(yù)選按鈕值
Struts2 include標(biāo)簽
Struts2 + Hibernate 集成實(shí)例
配置全局資源包
Struts2 異常處理
Struts2 &lt;s:set&gt;標(biāo)簽示例
Struts2 &lt;s:doubleselect&gt;示例
Struts2 iterator標(biāo)簽
optiontransferselect 示例
Struts2映射攔截動(dòng)作
head 示例
Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2命名空間配置和解釋
<s:param>標(biāo)簽示例
Struts2+Hibernate集成實(shí)例
Struts2 &lt;s:head&gt;示例
Struts2 &lt;s:push&gt;標(biāo)簽示例
Struts2文件上傳例子
Struts2 &lt;s:param&gt;標(biāo)簽示例
@ResultPath 注釋示例
Struts2 體系結(jié)構(gòu)
Struts2 execAndWait攔截器例子
Struts2 攔截器
Textarea - 文本域
Struts2教程
<s:push>標(biāo)簽示例
Struts2 文件上傳
Struts2 date 標(biāo)簽
Struts2 &lt;s:texttag&gt;標(biāo)簽示例
Struts2+Quartz2調(diào)度集成實(shí)例
Struts2開發(fā)模式
Struts2 autocompleter+JSON例子
Struts2 類型轉(zhuǎn)換
<s:url>標(biāo)簽示例
Struts2 Ajax 標(biāo)簽
Struts2 bean 標(biāo)簽
<s:iterator>標(biāo)簽迭代示例
Hidden隱藏值示例
在Struts2獲取HttpServletResponse對(duì)象
Struts2 主題和模板
Struts2自動(dòng)選擇下拉框的值
Struts2下載文件實(shí)例
在Struts2中獲取 HttpServletRequest
級(jí)聯(lián)選擇示例
Struts2 &lt;sx:datetimepicker&gt;示例
Struts2 include(包含)多個(gè)配置文件
Struts2 &lt;s:select&gt;下拉框例子
Struts2 Action/動(dòng)作
Struts2 &lt;s:textarea&gt;文本域?qū)嵗?/span>
Struts2+Spring集成實(shí)例
Struts2 &lt;s:updownselect&gt;示例
Struts2 property 標(biāo)簽
Struts2 &lt;s:url&gt;標(biāo)簽示例
Struts2 表單標(biāo)簽
Struts2資源包使用示例
subset tag example
Struts 2 ActionError &amp; ActionMessage Example
Struts2 MVC架構(gòu)
Struts2 if/else標(biāo)簽
Struts2 &lt;s:generator&gt;標(biāo)簽示例
<s:include>標(biāo)簽示例
Struts2開發(fā)者模式
<s:generator> 標(biāo)簽示例
復(fù)選框heckbox 示例
Struts2 text 標(biāo)簽
Radio 單選按鍵示例
Struts2 &lt;s:a&gt;標(biāo)簽示例
Struts2 key鍵屬性示例
Struts2設(shè)置多個(gè)復(fù)選框缺省值
Struts2 基于XML校驗(yàn)
Struts2 配置文件
Struts2 &lt;s:radio&gt;單選按鈕例子
Struts2 發(fā)送郵件Email
Struts2獲取HttpServletResponse實(shí)例
<s:sort>標(biāo)簽示例
Struts2 &lt;s:optiontransferselect&gt;示例
Struts2和Tiles集成
Struts2模型驅(qū)動(dòng)實(shí)例
Struts2的FilterDispatcher和StrutsPrepareAndExecuteFilter有什么不同?
<s:append>標(biāo)簽示例
組合框示例
Struts2 &lt;s:password&gt;密碼示例
datetimepicker 日期選擇
Struts2 &lt;s:iterator&gt;迭代器標(biāo)簽示例
TextBox文檔示例
Struts2 url標(biāo)簽
資源包使用示例
Struts2本地化和國(guó)際化
Struts2 &lt;s:subset&gt;標(biāo)簽示例
Struts2 + Hibernate使用“Full Hibernate Plugin"插件集成
Struts2和JSON實(shí)例
Struts2注解示例
Struts2 &lt;s:checkbox&gt;復(fù)選框例子
Struts2快速入門
<s:date>標(biāo)簽示例
Struts2 set 標(biāo)簽
<s:set>標(biāo)簽示例
Struts2 命名空間配置和解釋
Struts 2 hello world (XML版本)
&nbsp;預(yù)選單選按鈕
<s:a>標(biāo)簽示例
Struts2+Spring+Hibernate集成實(shí)例
Struts2 &lt;s:combobox&gt;組合框的例子
Struts2 if,elseif,else標(biāo)簽示例
Struts2 @ResultPath注釋示例
struts2 &lt;s:append&gt;標(biāo)簽示例
Struts2 &lt;s:textfield&gt;文本示例
多文件上傳示例
使用Struts2動(dòng)作
<s:debug>標(biāo)簽示例
在Struts2中如何獲取ServletContext對(duì)象
Struts2 &lt;s:include&gt;標(biāo)簽示例
Password 示例

Struts2 值棧/OGNL

值棧:

值棧是一個(gè)集合中的幾個(gè)對(duì)象保持下列對(duì)象提供的順序:

SN 對(duì)象& 描述
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的標(biāo)簽。有各種不同的標(biāo)簽在單獨(dú)的章節(jié)中,我們將學(xué)習(xí),用于獲取和設(shè)置Struts 2.0 的值棧。 ValueStack的對(duì)象里面可以得到動(dòng)作如下:

ActionContext.getContext().getValueStack()

一旦擁有了值對(duì)象,就可以用下面的方法來(lái)操縱該對(duì)象:

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:

對(duì)象圖形導(dǎo)航語(yǔ)言(OGNL)是一個(gè)功能強(qiáng)大的表達(dá)式語(yǔ)言是用來(lái)參考值棧上的數(shù)據(jù)和操縱。 OGNL也有助于在數(shù)據(jù)傳輸和類型轉(zhuǎn)換。

OGNL和JSP表達(dá)式語(yǔ)言很相似。 OGNL 基礎(chǔ)的理念是在 root或默認(rèn)的對(duì)象范圍內(nèi)。默認(rèn)或根對(duì)象的屬性,可以參考使用的標(biāo)記符號(hào)(井號(hào))。

如前所述,OGNL是基于上下文和Struts的構(gòu)建ActionContext 使用OGNL映射。ActionContext中映射包括以下:

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

  • session - 會(huì)話范圍的變量

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

  • request - 請(qǐng)求范圍的變量

  • parameters - 請(qǐng)求參數(shù)

  • atributes - 存儲(chǔ)的屬性頁(yè)面,請(qǐng)求,會(huì)話和應(yīng)用范圍

重要的是要明白,操作對(duì)象是始終可用值棧中的。所以,因此,如果動(dòng)作對(duì)象的屬性x和y有隨時(shí)供使用。

在ActionContext中的對(duì)象被稱為使用井號(hào)的符號(hào),但是,值棧中的對(duì)象可以被直接引用,例如,如果員工是一個(gè)動(dòng)作類的屬性,那么就可以得到如下參考:

  <s:property value="name"/>

來(lái)代替

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

如果會(huì)話中有一個(gè)屬性叫做“login”,可以找回如下:

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

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

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

本OGNL表達(dá)式是巧妙地的解釋 "red","yellow","green"為顏色,并此基礎(chǔ)上建立一個(gè)列表。 

OGNL表達(dá)式將被廣泛使用時(shí),在接下來(lái)的章節(jié)中,我們將研究不同的標(biāo)簽。因此,讓我們來(lái)看看它使用的一些例子在Form標(biāo)簽/標(biāo)簽/數(shù)據(jù)標(biāo)簽控制和Ajax標(biāo)簽。

ValueStack/OGNL 例子:

創(chuàng)建動(dòng)作:

讓我們考慮以下動(dòng)作類,當(dāng)我們?cè)L問值棧,然后設(shè)置幾個(gè)鍵,我們將在視圖,即訪問使用OGNL,JSP頁(yè)面。

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;
   }
}

其實(shí),Struts 2的值棧的頂部增加了動(dòng)作時(shí)執(zhí)行。所以,通常的方法是把東西值棧添加 getter/setter方法以使這些值在Action類,然后使用<s:property>標(biāo)簽來(lái)訪問值。以下是展示如何在struts ActionContext 中 ValueStack  工作。

創(chuàng)建視圖

讓我們創(chuàng)建以下JSP文件 helloWorld.jsp 的要 WebContent 文件夾。這個(gè)視圖將被顯示動(dòng)作返回“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"