Struts2 值栈/OGNL

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.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。如以下画面:

Hello World Input

现在在给定的文本框中输入任何单词,然后点击"Say Hello"按钮执行已定义的动作。现在,如果检查生成的日志,会发现下面的文本底部:

 
Size of the valueStack: 3

这将显示以下画面,这将显示任何的值,将进入值为key1和key2,我们已经把它们放入 ValueStack。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值