JAVA中的OGNL:

1 调用对象的方法:

2 访问对象的静态方法:

3 获取OGNLContext、Root中的数据。

User:

4 访问Context:

关键还是在Struts2环境中的使用:

并没有打印 静态方法的值,因为Struts2默认关闭了。

再次刷新后发现有值了。

编写demo

debug启动 打断点发现: root和context均在这里面。

修改demo返回值

运行:

方式1:

方式2:

如果有多个user 默认展示栈顶的!!!

没有set get方法 无法查看 但是已经在栈顶了。

获取值栈的数据:

在debug里可以找到:

request中找不到 就自动去值栈中寻找。

修改crm

jsp中去掉jstl  引入struts标签库 然后不用foreach循环 改用s标签 。

今日代码:

package com.itheima.struts2.valuestack;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; /**
* ValueStack内部结构
* @author ZWT
*
*/
public class ValueStackDemo1 extends ActionSupport{ @SuppressWarnings("unused")
@Override
public String execute() throws Exception {
//获得值栈
ValueStack valueStack = ActionContext.getContext().getValueStack(); return SUCCESS;
} }

  

package com.itheima.struts2.valuestack;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; /**
* 获得ValueStack对象
* @author ZWT
*
*/
public class ValueStackDemo2 extends ActionSupport{ @SuppressWarnings("unused")
@Override
public String execute() throws Exception {
//获得值栈
ValueStack valueStack1 = ActionContext.getContext().getValueStack(); //2
ValueStack valueStack2 = (ValueStack) ServletActionContext.getRequest().getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
//一个action实例只会创建一个valuestack
System.out.println(valueStack1 == valueStack2 );
return NONE;
} }

  

package com.itheima.struts2.valuestack;

import org.apache.struts2.ServletActionContext;

import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; /**
* 操作ValueStack
* @author ZWT
*
*/
public class ValueStackDemo3 extends ActionSupport{ private User user; public User getUser() {
return user;
} @SuppressWarnings("unused")
@Override
public String execute() throws Exception {
user = new User("张飞","222"); return SUCCESS;
} }

  

package com.itheima.struts2.valuestack;

import org.apache.struts2.ServletActionContext;

import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; /**
* 操作ValueStack :方式2 调用值栈中的方法实现
* @author ZWT
*
*/
public class ValueStackDemo4 extends ActionSupport{ @SuppressWarnings("unused")
@Override
public String execute() throws Exception { ValueStack valueStack1 = ActionContext.getContext().getValueStack();
//使用push(Object) set(String key, Object obj)
User user = new User("张飞","56");
//user现在在栈顶位置
valueStack1.push(user);
//创建map集合 map压入栈顶
valueStack1.set("name", "关羽");
return SUCCESS;
} }

  

package com.itheima.struts2.valuestack;

import java.util.ArrayList;
import java.util.List; import org.apache.struts2.ServletActionContext; import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; /**
* 获取值栈的数据
* @author ZWT
*
*/
public class ValueStackDemo5 extends ActionSupport{ @SuppressWarnings("unused")
@Override
public String execute() throws Exception { ValueStack valueStack1 = ActionContext.getContext().getValueStack();
//向值栈中保存一个对象 获取出来
User user = new User("张飞","123");
valueStack1.push(user);
//保存集合
List<User> list = new ArrayList<User>();
list.add(new User("aaa","111"));
list.add(new User("bbb","222"));
list.add(new User("ccc","333"));
valueStack1.set("list", list); //往context中存
ServletActionContext.getRequest().setAttribute("name", "www");
ServletActionContext.getRequest().getSession().setAttribute("name", "eee");
ServletActionContext.getServletContext().setAttribute("name", "rrr");
return SUCCESS;
} }

  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo1" extends="struts-default" namespace="/">
<action name="valueStackDemo1" class="com.itheima.struts2.valuestack.ValueStackDemo1">
<result>/demo1/success.jsp</result>
</action>
<action name="valueStackDemo2" class="com.itheima.struts2.valuestack.ValueStackDemo2">
</action>
<action name="valueStackDemo3" class="com.itheima.struts2.valuestack.ValueStackDemo3">
<result>/demo1/success.jsp</result>
</action>
<action name="valueStackDemo4" class="com.itheima.struts2.valuestack.ValueStackDemo4">
<result>/demo1/success.jsp</result>
</action> <action name="valueStackDemo5" class="com.itheima.struts2.valuestack.ValueStackDemo5">
<result>/demo1/success2.jsp</result>
</action> </package>
</struts>

  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置Struts2的常量 -->
<constant name="struts.action.extension" value="action"/>
<!--开启静态方法访问 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <include file="com/itheima/struts2/valuestack/struts_demo1.xml"></include> </struts>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>OGNL在Struts2环境中的入门</h1>
<h3>调用对象的方法</h3>
<s:property value="'struts'.length()"/>
<h3>调用对象的静态方法</h3>
<s:property value="@java.lang.Math@random()"/> </body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>查看值栈的内部结构</h1>
<s:debug></s:debug>
<!-- 方式1获取 利用action在值栈中的特性 -->
<%-- <s:property value="user.username"/>
<s:property value="user.password"/> --%> <!-- 方式2获取 利用valuestack本身方法 -->
<s:property value="username"/>
<s:property value="password"/>
<s:property value="name"/> </body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>查看值栈的内部结构</h1>
<s:debug></s:debug>
<!--获取对象的属性 -->
<s:property value="username"/>
<s:property value="password"/> <!-- 获取集合当中的数据 -->
<s:property value="list[0].username"/>
<s:property value="list[0].password"/>
<s:property value="list[1].username"/>
<s:property value="list[1].password"/>
<s:property value="list[2].username"/>
<s:property value="list[2].password"/> <!-- 获取context中的数据 -->
<s:property value="#request.name"/>
<s:property value="#session.name"/>
<s:property value="#application.name"/> <hr>
${ username } </body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>#号的用法</h1>
<h3>用途1:获取context的数据</h3>
<%
request.setAttribute("name","李兵");
%>
<s:property value="#request.name"/> <h3>构建map集合</h3>
<!-- list i只要定义 context中也有-->
<s:iterator var="i" value="{'aa','bb','cc'}">
<s:property value="i"/>--<s:property value="#i"/><br>
</s:iterator>
<!-- map -->
<s:iterator var="entry" value="#{'aa':'11','bb':'22','cc':'33' }">
<s:property value="key"/>--<s:property value="value"/><br>
<s:property value="#entry.key"/>--<s:property value="#entry.value"/><br>
</s:iterator>
<hr>
<!-- value值 和 外面的‘男’ 一样就用list 不然用map -->
<%-- 性别:<input type="radio" name="sex1" value="男">男
<input type="radio" name="sex1" value="女">女
<hr>
<s:radio list="{'男','女'}" name="sex2" label="姓名" /> --%>
<hr>
性别:<input type="radio" name="sex1" value="1">男
<input type="radio" name="sex1" value="2">女
<hr>
<s:radio list="#{'1':'男','2':'女'}" name="sex2" label="姓名" /> </body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
pageContext.setAttribute("basePath", basePath);%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>%号的用法</h1>
<h3></h3>
<%
request.setAttribute("name", "张飞");
%>
<s:property value="#request.name"/><br>
姓名:<s:textfield name="name" value="%{#request.name}"></s:textfield><br>
<s:property value="%{'#request.name'}"/><br>
</body>
</html>

  

Struts2学习第3天--OGNL、EL、值栈的更多相关文章

  1. Struts工作机制图+OGNL+EL+值栈(Map,对象栈)

    struts 值栈  通过get set方法 方便的获取,设置属性值      比如从jsp页面传来的參数...从Action设置jsp所要回显的内容 注意EL表达式,struts2对request进 ...

  2. Struts2笔记3--获取ServletAPI和OGNL与值栈

    获取ServletAPI: 第一种方式: //在request域中放入属性req,暂且认为getContext()获取的是request域空间,但实际不是 ActionContext.getConte ...

  3. struts2中,OGNL访问值栈的时候查找的顺序是什么?请排序:模型对象、临时对象、固定名称的对象、Action对象

    struts2中,OGNL访问值栈的时候查找的顺序是什么?请排序:模型对象.临时对象.固定名称的对象.Action对象 解答:struts2的值栈排列顺序为:1).临时对象:2).模型对象:3).Ac ...

  4. (转)OGNL与值栈

    http://blog.csdn.net/yerenyuan_pku/article/details/67709693 OGNL的概述 什么是OGNL 据度娘所说: OGNL是Object-Graph ...

  5. 框架学习之Struts2(三)---OGNL和值栈

    一.OGNL概述 1.1OGNL是对象图导航语言(Object-Graph Navigation Languaged)的缩写,他是一种功能强大的表达式语言,通过简单一致的表达式语法,可以存取Java对 ...

  6. Struts2学习(四)———— ognl表达式、值栈、actionContext之间的关系

    一.什么是Ognl? 通过百度百科查询到的解释,其中详细的说明了OGNL的作用. 下面我们就对OGNL这5个作用进行讲解 1.存取对象的任意属性,简单说就是对javabean进行操作(重要) 2.调用 ...

  7. Struts2 (三) — OGNL与值栈

    一.OGNL表达式 1.概述 1.1什么是OGNL ​ OGNL是Object-Graph Navigation Language的缩写,俗称对象图导航语言. 它是一种功能强大的表达式语言,通过它简单 ...

  8. EL与OGNL以及值栈的理解

    这里先添加下在项目遇到的问题: 这两天在做论坛项目的时候,犯了一个错误:将数据放入值栈中,结果jsp页面获取不到. 困扰了许久: 总结如下: (1)每个action对应相应页面的值栈中值的获取,在属于 ...

  9. Struts2学习第七课 OGNL

    request变成了struts重写的StrutsRequestWrapper 关于值栈: helloWorld时,${productName}读取productName值,实际上该属性并不在requ ...

随机推荐

  1. 事件调度器及C++中的使用

    转自:http://blog.ch-wind.com/ue4-event-dispatcher-and-delegate/ 事件调度器非常的适合在各个蓝图之间实现通信功能. 当前UE4版本4.8.3. ...

  2. python学习(十八) 程序打包

    18.1  Distutils基础 18.2 打包 18.2.1 建立存档文件 18.2.2 创建Windows安装程序或RPM包 18.3 编译扩展 18.4 使用py2exe创建可执行程序

  3. Py修行路 python基础(一)初识

    编译:把铭文代码执行前,先转成二进制,再执行,这个过程就叫编译. 编译型c,c++go特点:运行效率高依赖编译平台,linux 操作系统 跟CPU交互的接口,与windows不是完全一样不能跨平台,开 ...

  4. 第六章 MySQL函数(待续)

    ············

  5. java多线程编程核心技术-笔记

    一.第一章 1.自定义线程类中实例变量针对其他线程有共享和不共享之分,自定义线程中的变量,如果是继承自thread类,则每个线程中的示例变量的更改,不影响其他线程2.当多个线程去访问一个局部变量是会产 ...

  6. jdk环境变量一键设置 管理員运行

    退出360等杀毒软件 本机win10 其他环境自测.参考了网上代码修改. @echo off rem dss color 02 mode con cols=70 lines=30 title JDK ...

  7. Screen - BOM对象

    Screen 对象 Screen 对象包含有关客户端显示屏幕的信息. 注释:没有应用于 screen 对象的公开标准,不过所有浏览器都支持该对象. Screen 对象属性 属性 描述 availHei ...

  8. duck typing

    在程序设计中,鸭子类型(英语:duck typing)是动态类型的一种风格.在这种风格中,一个对象有效的语义,不是由继承自特定的类或实现特定的接口,而是由"当前方法和属性的集合"决 ...

  9. Zookeeper Api(java)入门与应用

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  10. linux单用户模式修改密码

    Linux使用版本: Centos 7 救援模式: 1,在虚拟机设置里查看光驱是否开启启动,要保证设置状态里的两个选项都已选择. 2,先将Centos系统关机,然后在VMware左侧选中这台虚拟机并右 ...