OGNL:

对象图导航语言(OGNL)是一个功能强大的表达式语言,用于引用和操作数据的值栈。OGNL还可以在数据传输和类型转换。
OGNL是非常相似的JSP表达式语言。 OGNL是基于的思想具有根或缺省对象的范围内的。默认的根对象的属性可以参考使用的标记符号,这是英镑符号。

正如前面提到的,OGNL根据上下文和Struts建立一个ActionContext中使用OGNL地图。ActionContext中的地图由下列组成:

  1. application - 应用程序范围内的变量

  2. session - 会话范围的变量

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

  4. request - 请求范围的变量

  5. parameters - 请求参数

  6. 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表达式将被广泛使用在接下来的章节中,我们将研究不同的标签。因此,而不是孤立地看着他们,让我们来看看的表格标签/控制标签/数据标签和Ajax标签部分在使用中的一些例子。

值栈/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>的标签,访问值。但我展示你究竟是如何的ActionContext中和值栈在struts的工作。

创建视图

让我们创建下面的JSP文件helloWorld.jsp,在WebContent文件夹在eclipse项目。这种观点的情况下采取行动的成功返回,将显示:

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

右键点击项目名称,并单击“导出”> WAR文件创建一个WAR文件。然后,这WAR部署在Tomcat的webapps目录下。最后,启动Tomcat服务器,并尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp。这会给你以下画面:

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

 
Size of the valueStack: 3

这将显示下面的屏幕,它会显示任何价值,你将进入和值key1和key2,我们赋上了值栈。

猜你喜欢

转载自www.cnblogs.com/borter/p/9502153.html