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对象中都存在 ...
随机推荐
- Linux服务器上安装tomcat
安装软件 : apache-tomcat-9.0.0.M1.tar.gz(下载地址http://tomcat.apache.org/) 步骤一 Tomcat是其中一个开源的且免费的java Web服务 ...
- zhx'code1
void dandiao() { ,tail=; ;a<=k;a++) { ]) tail--; tail++; q[tail][]=z[a];q[tail][]=a; } ;a<=n;a ...
- New Concept English there (8)
31w/m 56% The Great St Bernard Pass connects Switzerland to Italy. At 247o metres, it is the highest ...
- Java静态绑定和动态绑定
程序绑定的概念: 绑定指的是一个方法的调用与方法所在的类(方法主体)关联起来.对java来说,绑定分为静态绑定和动态绑定:或者叫做前期绑定和后期绑定 静态绑定(早绑定 编译器绑定): 在程序执行前方法 ...
- ThinkPHP 连接数据库
今天在配置连接Mysql 时出现了bool(false)的提示,仔细修改了mysql的密码,还是不对,发现问题应该数据库本身设置了前缀tb_本身的拼写错误和注释掉了 'DB_PREFIX'=>' ...
- Servlet自动加载
--初始化数据库连接 在<servlet>标签下 <servlet-name>ServletDemo</servlet-name> <servlet-clas ...
- D3.js 使用心得
教程 D3.js 入门教程系列 http://www.ourd3js.com/wordpress/296/ 全球地图数据
- HDU 数位dp
模板http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html 完全理解以后,我发现这种写法实在是太厉害了,简洁,优美,可以回避很多细节 ...
- onerror="javascript:this.src='images/defaultUpload.png';"引发的死循环错误
18:10:47.441 WARN o.s.web.servlet.PageNotFound:1101 - No mapping found for HTTP request with URI [/c ...
- Codeforces 311B Cats Transport【斜率优化DP】
LINK 题目大意 有一些猫,放在一些位置,人一步移动一个位置 给出每个猫出现的时间,每个人可以自由安排其出发时间,沿途已经出现的猫捡起,猫等待的时间是被减去的时间减去出现的时间 猫可以等人,人不能等 ...