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对象中都存在 ...
随机推荐
- docker 快速搭建 WordPress
安装Docker 环境:阿里云服务器 镜像:CentOs 7.4 64 https://docs.docker.com/install/linux/docker-ce/centos/ 安装WordPr ...
- Java线程的五种状态详解
状态转换图 1.new状态:通过new关键字创建了Thread或其子类的对象 2.Runnable状态:即就绪状态.可从三种状态到达,new状态的Thread对象调用start()方法,Running ...
- New Concept English there (7)
27w/m Has it ever happened to you? Have you ever put your trousers in the washing machine and then r ...
- LINUX中的DNS服务---高速缓存DNS
一.什么是DNS Domain Name System,域名系统. 万维网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网.他主要负责把域名和IP的相互转换 ...
- 在servlet中的中文乱码,相对路径和绝对路径
默认情况下在servlet中的中文是显示不出来的,解决问题就是加resp.setContentType("text/html;charset=gbk"); 而且这句加的话必须写在P ...
- 正则 匹配 HTML 标签
var tt=((result.data).toString()).match(/<style(([\s\S])*?)<\/style>/g);
- python2 之 pyh2
1.功能 pyh作爲基於python的簡易html生成庫,收到了廣大python愛好者(比如說我,當然其他人沒有調查過)的深切喜愛. 簡潔的行文風格繼承了python一貫的作風,可以讓你用簡單的Pyt ...
- 深入理解java虚拟机-第13章-线程安全与锁优化
第十三章 线程安全与锁优化 线程安全 java语言中的线程安全 1 不可变.Immutable 的对象一定是线程安全的 2 绝对线程安全 一个类要达到不管运行时环境如何,调用者都不需要额外的同步措施, ...
- ios PageControl and UIScrollView
// // AlbumViewController.m // HwangKop08.18 // // Created by rimi on 15/8/20. // Copyright (c) ...
- redis整合异常总结
问题:org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field ' ...