所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)

第一节:值栈简介

值栈是对应每个请求对象的一套内存数据的封装,Struts2 会给每个请求创建一个新的值栈。

值栈能够线程安全地为每个请求提供公共的数据存取服务。

第二节:OGNL 引入

OGNL 是对象图导航语言Object-Graph Navigation Language 的缩写,它是一种功能强大的表达式语言。

OGNL 访问ValueStack 数据

  <s:property value=”account” />

  注:这里要添加<%@taglib prefix="s" uri="/struts-tags" %>struts的标签库

OGNL 访问ActionContext 数据

  访问某个范围下的数据要用#

    #parameters 请求参数request.getParameter(...);

    #request 请求作用域中的数据request.getAttribute(...);

    #session 会话作用域中的数据session.getAttribute(...);

    #application 应用程序作用域中的数据application.getAttribute(...);

    #attr 按照page request session application 顺序查找值

例子:

 struts.xml
1 <?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> <package name="manage" namespace="/" extends="struts-default"> <action name="hello" class="com.wishwzp.action.HelloAction">
<result name="success" >success.jsp</result>
</action>
</package> </struts>
 HelloAction.java
1 package com.wishwzp.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=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;
}
}
 success.jsp
1 <%@ 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");
%>
</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>

url:http://localhost:8080/Struts2Chap04/hello?name=ssss&age=23

结果:

第三节:OGNL 访问复杂对象

1,访问javaBean 对象;

2,访问集合对象;

3,访问Map 对象;

例子:

 struts.xml
1 <?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> <package name="manage" namespace="/" extends="struts-default"> <action name="hello" class="com.wishwzp.action.HelloAction">
<result name="success" >success.jsp</result>
</action>
</package> </struts>
 Student.java
1 package com.wishwzp.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;
} }
 HelloAction.java
1 package com.wishwzp.action; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.wishwzp.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; //访问javaBean对象,并get...和set...
private Student student; //访问集合对象,并get...和set...
private List<Student> students; //访问Map对象,并get...和set...
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 { //javaBean对象
student=new Student("小扒", 12); //集合对象
students=new ArrayList<Student>();
students.add(new Student("老九",13));
students.add(new Student("老十",14)); //Map对象
studentMap=new HashMap<String,Student>();
studentMap.put("goodStudent", new Student("学霸",20));
studentMap.put("badStudent", new Student("学渣",19));
return SUCCESS;
} }
 success.jsp
1 <%@ 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"/><br/>
<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"/><br/>
<s:property value="studentMap['badStudent'].name"/>
<s:property value="studentMap['badStudent'].age"/><br/>
</body>
</html>

url访问:http://localhost:8080/Struts2Chap04/hello

结果:

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

1,访问静态属性;

2,访问静态方法;需要在struts.xml加上<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

例子:

 struts.xml
1 <?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.ognl.allowStaticMethodAccess" value="true"></constant> <package name="manage" namespace="/" extends="struts-default"> </package> </struts>
 MyStatic.java
1 package com.wishwzp.common; public class MyStatic { //静态属性
public static final String str="Struts2开心学习"; //静态方法
public static String printUrl(){
System.out.println("http://www.baidu.com");
return "http://www.baidu.com";
}
}
 ognl_static.jsp
1 <%@ 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>
访问静态属性: <s:property value="@com.wishwzp.common.MyStatic@str"/><br/>
访问静态方法:<s:property value="@com.wishwzp.common.MyStatic@printUrl()"/>
</body>
</html>

url:http://localhost:8080/Struts2Chap04/ognl_static.jsp

结果:

(四)值栈与OGNL的更多相关文章

  1. Struts2的值栈和OGNL牛逼啊

    Struts2的值栈和OGNL牛逼啊 一 值栈简介: 值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务. 二 ...

  2. Struts2知识点小结(三)--值栈与ognl表达式

    1.问题一 : 什么是值栈 ValueStack        回顾web阶段 数据交互问题?        客户端提交数据  到  服务器端    request接受数据+BeanUtils实体封装 ...

  3. 值栈与ognl

    ValueStack (值栈): 1.贯穿整个Action的生命周期(每个Action类的对象实例都拥有一个ValueStack对象).相当于一个数据的中转站.在其中保存当前Action对象和其他相关 ...

  4. 关于Struts2中的值栈与OGNL表达式

    1.1.1    OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL     :OGNL比EL功能强大很多倍. 它是一个开源项目. ...

  5. 值栈和OGNL 之 7.1 值栈

    7.1  值栈 7.1.1  值栈是什么 简单的说:值栈是对应每一个请求对象的轻量级的内存数据中心. Struts2中一个很激动人心的特性就是引入了值栈,在这里统一管理着数据,供Action.Resu ...

  6. Struts2基础学习(七)—值栈和OGNL

    目录: 一.值栈 二.OGNL表达式 一.值栈(ValueStack) 1.定义      ValueStack贯穿整个Acton的生命周期,每个Action类的对象实例都拥有一个ValueStack ...

  7. Struts(九):值栈(OGNL)

    引言 在我们开发过程中,往往会使用一个对像传递到一个具体的action中,之后到跳转页面中访问对应对象的具体的参数. 比如:我们搭建一个struts2项目: 回顾下如何搭建strut2: 1.下载的s ...

  8. 学习Struts--Chap05:值栈和OGNL

    1.值栈的介绍 1.1 值栈的介绍: 值栈是对应每一个请求对象的数据存储中心,struts2会给每一个请求对象创建一个值栈,我们大多数情况下不需要考虑值栈在哪里,里面有什么,只需要去获取自己需要的数据 ...

  9. 走进Struts2(五)— 值栈和OGNL

    值栈 1.值栈是什么? 简单说:就是相应每个请求对象的轻量级的内存数据中心. Struts2引入值栈最大的优点就是:在大多数情况下,用户根本无须关心值栈,无论它在哪里,不用管它里面有什么,仅仅须要去获 ...

随机推荐

  1. 数学之美 zt

    数学是美丽的,哪里有数哪里就有美. 数学的定义是研究数量关系和空间形式的一门科学.但有句名言说:数学比科学大得多,因为它是科学的语言.数学不仅用来写科学,而且可用来写人生.所以说数学是一切学科的基础, ...

  2. Python 实现网络爬虫小程序

    Python很简洁,也很强大,作为兴趣,值得一学!   下面这个程序实现的是从一个网站上下载图片,根据自己需要可以进行修改 import re import urllib def gethtml(ur ...

  3. HDU-1233 还是畅通工程 (prim 算法求最小生成树)

    prim 算法求最小生成树 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. 解决eclipse插件svn不显示svn信息和显示的信息为数字的问题

    1.选择window-->preferences如下图 通过上面步骤svn信息便显示了 2.解决显示的信息为数字问题 选择svn-label decoration format里面的author ...

  5. It appears as though you do not have permission to view information for any of the services you requested

  6. Shell if else语句

    if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句: if ... else ... fi 语句: if ... ...

  7. hdoj 4738 Caocao's Bridges【双连通分量求桥】

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdoj 5443 The Water Problem【线段树求区间最大值】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5443 刷道水题助助兴 #include<stdio.h> #include<stri ...

  9. 【SQL】MySQL内置函数中的字符串函数和日期时间函数

    字符串函数 --拼接字符串组成新的字符串 Select concat(‘A’,’B’); --返回字符串长度 Select length(“CAT”) --返回子字符串在字符串中首次出现的位置,没有返 ...

  10. 一步一步学android控件(之二十五)—— SeekBar

    SeekBar扩展自ProgressBar——在ProgressBar的基础上添加了一个用户可以拖拽的thum. SeekBar.OnSeekBarChangeListener是接收SeekBar进度 ...