详解Java的Struts框架中栈值和OGNL的使用

值栈:
值栈是一个集合中的几个对象保持下列对象提供的顺序:

20151126155528201.png (590×348)

值栈可以通过JSP,Velocity或者Freemarker的标签。有各种不同的标签在单独的章节中,我们将学习,用于获取和设置Struts 2.0 的值栈。 ValueStack的对象里面可以得到动作如下:

ActionContext.getContext().getValueStack()
一旦拥有了值对象,就可以用下面的方法来操纵该对象:
20151126155550681.png (581×604)

OGNL:
对象图形导航语言(OGNL)是一个功能强大的表达式语言是用来参考值栈上的数据和操纵。 OGNL也有助于在数据传输和类型转换。

OGNL和JSP表达式语言很相似。 OGNL 基础的理念是在 root或默认的对象范围内。默认或根对象的属性,可以参考使用的标记符号(井号)。

如前所述,OGNL是基于上下文和Struts的构建ActionContext 使用OGNL映射。ActionContext中映射包括以下:

application - 应用范围的变量网址:yii666.com<

session - 会话范围的变量文章地址https://www.yii666.com/blog/114957.html

root / value stack - 所有操作变量都保存在这里

request - 请求范围的变量

parameters - 请求参数

atributes - 存储的属性页面,请求,会话和应用范围

重要的是要明白,操作对象是始终可用值栈中的。所以,因此,如果动作对象的属性x和y有随时供使用。

在ActionContext中的对象被称为使用井号的符号,但是,值栈中的对象可以被直接引用,例如,如果员工是一个动作类的属性,那么就可以得到如下参考:

 <s:property value="name"/>

来代替

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

如果会话中有一个属性叫做“login”,可以找回如下:网址:yii666.com

 <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。如以下画面:
文章来源地址https://www.yii666.com/blog/114957.html

20151126155820122.jpg (560×285)文章来源地址:https://www.yii666.com/blog/114957.html

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

 
Size of the valueStack: 3

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

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

领支付宝红包赞助服务器费用

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png