struts2学习(7)值栈简介与OGNL引入
一、值栈简介:

二、OGNL引入:

com.cy.action.HelloAction.java:
package com.cy.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; public class HelloAction extends ActionSupport{
private static final long serialVersionUID = 1L; @Override
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
ValueStack valueStack = actionContext.getValueStack();
valueStack.set("name", "张三(valueStack)");
valueStack.set("age", 11); Map<String, Object> session = actionContext.getSession();
session.put("name", "王五(session)");
session.put("age", 13); Map<String, Object> application = actionContext.getApplication();
application.put("name", "赵六(application)");
application.put("age", 14); return SUCCESS;
} }
struts.xml:
<struts>
<package name="manage" namespace="/" extends="struts-default">
<action name="hello" class="com.cy.action.HelloAction">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
request.setAttribute("name", "李四(request)");
request.setAttribute("age", 12); pageContext.setAttribute("name", "小沛(page)");
pageContext.setAttribute("age", "18");
%>
</head>
<body>
获取狭义上的值栈数据:<s:property value="name"/>
<s:property value="age"/><br>
请求参数:<s:property value="#parameters.name"/>
<s:property value="#parameters.age"/><br>
request:<s:property value="#request.name"/>
<s:property value="#request.age"/><br>
session:<s:property value="#session.name"/>
<s:property value="#session.age"/><br>
application:<s:property value="#application.name"/>
<s:property value="#application.age"/><br>
attr取值:<s:property value="#attr.name"/>
<s:property value="#attr.age"/><br>
</body>
</html>
测试结果:

三、OGNL访问复杂对象:

四、OGNL访问静态属性和静态方法:

com.cy.model.Student.java:
package com.cy.model;
public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
com.cy.common.MyStatic.java:静态属性和静态方法:
package com.cy.common;
public class MyStatic {
public static final String str = "好好学习";
public static String print(){
return "天天向上";
}
}
com.cy.action.HelloAction.java中存入javaBean、List、Map:
package com.cy.action; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.cy.model.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; public class HelloAction extends ActionSupport{
private static final long serialVersionUID = 1L; private Student student;
private List<Student> students;
private Map<String,Student> studentMap; public Map<String, Student> getStudentMap() {
return studentMap;
} public void setStudentMap(Map<String, Student> studentMap) {
this.studentMap = studentMap;
} public List<Student> getStudents() {
return students;
} public void setStudents(List<Student> students) {
this.students = students;
} public Student getStudent() {
return student;
} public void setStudent(Student student) {
this.student = student;
} @Override
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
ValueStack valueStack = actionContext.getValueStack();
valueStack.set("name", "张三(valueStack)");
valueStack.set("age", 11); Map<String, Object> session = actionContext.getSession();
session.put("name", "王五(session)");
session.put("age", 13); Map<String, Object> application = actionContext.getApplication();
application.put("name", "赵六(application)");
application.put("age", 14); student = new Student("小八", 15); students=new ArrayList<Student>();
students.add(new Student("老九",13));
students.add(new Student("老十",14)); studentMap=new HashMap<String,Student>();
studentMap.put("goodStudent", new Student("学霸",20));
studentMap.put("badStudent", new Student("学渣",19)); return SUCCESS;
} }
struts.xml配置,开启ognl允许访问静态方法:
<struts>
<!-- 允许OGNL访问静态方法 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <package name="manage" namespace="/" extends="struts-default">
<action name="hello" class="com.cy.action.HelloAction">
<result name="success">success.jsp</result>
</action> </package> </struts>
success.jsp,通过ognl来取值:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
ognl访问javaBean对象:<s:property value="student.name"/>
<s:property value="student.age"/><br>
ognl访问List集合:<s:property value="students[0].name"/>
<s:property value="students[0].age"/>
<s:property value="students[1].name"/>
<s:property value="students[1].age"/><br/>
ognl访问Map:<s:property value="studentMap['goodStudent'].name"/>
<s:property value="studentMap['goodStudent'].age"/>
<s:property value="studentMap['badStudent'].name"/>
<s:property value="studentMap['badStudent'].age"/><br/> ognl访问静态属性:<s:property value="@com.cy.common.MyStatic@str"/><br>
<!-- 访问静态方法,有些封装好的Util工具,转换等,就可以直接调用了 -->
ognl访问静态方法:<s:property value="@com.cy.common.MyStatic@print()"/>
</body>
</html>
测试:

---------
struts2学习(7)值栈简介与OGNL引入的更多相关文章
- Struts2学习:值栈(value stack)
1.index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %& ...
- Struts2基础学习(七)—值栈和OGNL
目录: 一.值栈 二.OGNL表达式 一.值栈(ValueStack) 1.定义 ValueStack贯穿整个Acton的生命周期,每个Action类的对象实例都拥有一个ValueStack ...
- 关于Struts2中的值栈与OGNL表达式
1.1.1 OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL :OGNL比EL功能强大很多倍. 它是一个开源项目. ...
- 学习Struts--Chap05:值栈和OGNL
1.值栈的介绍 1.1 值栈的介绍: 值栈是对应每一个请求对象的数据存储中心,struts2会给每一个请求对象创建一个值栈,我们大多数情况下不需要考虑值栈在哪里,里面有什么,只需要去获取自己需要的数据 ...
- struts2中各种值栈问题
struts2中OGNL和 ValueStack(一) 收藏 学习的时候,总分不清楚在struts2中页面的传值和取值是怎么来完成的,所以从网上搜了很多资料,现在把这些资料总结写,留着以后参考..看完 ...
- Struts2 中的值栈的理解
通过对struts2的一段时间的接触,将自己对OGNL的核心值栈说说,值栈:简单的说,就是存放action的堆栈,当我们提交一个请求 道服务器端 action时,就有个堆栈,如果action在服务器端 ...
- Struts学习之值栈的理解
转自:http://blog.csdn.net/hanxuemin12345/article/details/38559979 页面一个请求发送过来,依次经过一系列拦截器(处理公共部分,如:往数据中心 ...
- struts2中的值栈对象ValueStack
ValueStack, 即值栈对象. 值栈对象: 是整个struts数据存储的核心,或者叫中转站. 用户每次访问struts的action,都会创建一个Action对象.值栈对象.ActionCont ...
- Struts2中的值栈
一 什么是值栈 值栈: struts2中提供的一种类似于域对象的工具, 用于struts2中的存值和取值. 每次访问Action的时候, 都会创建一个action对象, 而每个action对象中都存在 ...
随机推荐
- 关于javascript严格模式下七种禁止使用的写法
分享至javascript语言精髓与编程实践 开启严格模式(”use strict"): 在全局代码的开始处加入 在eval代码的开始处加入 在函数声明代码处加入 在new Function ...
- FMDB给表添加新的字段
1.首先判断添加的字段是否存在,如果不存在就添加. 2.代码演示: (1)判断是否存在,判断之前先导入头文件确保可以调用FMDB的api(#import “FMDatabaseAdditions.h” ...
- 关于Spinlock机制的一点思考
存在两段代码同时在多核上执行的情况,这时候才需要一个真正的锁来宣告代码对资源的占有. 几个核可能会同时access临界区,这时的spinlock是如何实现的呢? 要用到CPU提供的一些特殊指令,对lo ...
- spring boot 教程(一) 构建我的第一个Spring boot
Spring Boot特点 1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自动配置Spring 5. 提供生产就绪型功能,如指标, ...
- EasyDSS RTMP流媒体服务器中调用videojs播放rtmp视频显示在左上角问题
本文转自EasyDarwin团队成员Penggy的博客:http://www.jianshu.com/p/f63f5b7c691b 问题描述: 近期我开发了一款新一代的RTMP/HLS流媒体服务器软件 ...
- grunt实现修改代码实时刷新浏览器
grunt例子:https://github.com/Aquarius1993/gruntDemo grunt 实时刷新1: 1.安装chrome浏览器插件:liveReload ...
- 关于鼠标不敏感导致自以为ubuntu很怪的问题
你要相信自己拥有的确实是一个垃圾鼠标,而不要以为复制和粘贴有感觉控制不住.
- js之10天内免登陆
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用阿里云加速docker镜像的安装
刚接触docker,尝试安装node镜像.docker运行在win7中,安装完Docker Toolbox之后简单敲了docker pull node命令,然后就是漫长的等待了… 等待的结果就是nod ...
- my_itoa
#include <iostream> using namespace std; char *my_reverse(char* s) { char *p,*q; p=s;q=s; whil ...