Struts2的值栈和OGNL牛逼啊
Struts2的值栈和OGNL牛逼啊
一 值栈简介:
值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务。
二 OGNL介绍:
(1)基本数据:
OGNL 是对象图导航语言 Object-GraphNavigationLanguage 的缩写,它是一种功能强大的表达式语言。
OGNL 访问 ValueStack 数据 <s:propertyvalue=”account”/>
OGNL 访问 ActionContext 数据
访问某个范围下的数据要用#
#parameters 请求参数 request.getParameter(...);
#request 请求作用域中的数据 request.getAttribute(...);
#session 会话作用域中的数据 session.getAttribute(...);
#application 应用程序作用域中的数据 application.getAttribute(...);
#attr 按照 page request session application 顺序查找值
我们以例子理解这部分内容,设置HelloAction:
 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);
         //session中的值
         Map<String, Object> session=actionContext.getSession();
         session.put("name","王五(session)");
         session.put("age","13");
         //application中的内容
         Map<String, Object> application=actionContext.getApplication();
         application.put("name", "赵六(application)");
         application.put("age","14");
         return SUCCESS;
     }
 }
Struts.xml文件的配置:
<struts>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="manage" namespace="/" extends="struts-default">
<action name="hello" class="com.java1234.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>
</head>
<%
request.setAttribute("name", "李四(request)");
request.setAttribute("age", "12");
%>
<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>
首先,是取值方式<s:property value="方式"/>,
①值栈 直接取 比如说是name age 就可以使用这种方式 value=”name” value=”age”
②page页面传递的数据 比如说是name age 使用这种方式 value="#parameters.name” value="#parameters.age”
③requset 设置的值 使用的方式 value="#request.name" value="#request.age"
④session设置的值使用的方式 value="#session.name" value="#session.age"
⑤application设置的值使用的方式 value="#application.name" value="#application.age"
之后attr的取值方式是按照 page request session applicaiton这个顺序取得。
例如:attr获取的是request的值

(2)OGNL 访问静态方法和属性
Mystatic类:
 public class MyStatic {
 public static final String str="yxs";
 public static String printUrl(){
 System.out.println("http://www.yxs.com");
 return "http://www.yxs.com";
 }
 }
Static.jsp直接访问:
<body>
访问静态属性: <s:property value="@com.java1234.common.MyStatic@str"/><br/>
访问静态方法:<s:property value="@com.java1234.common.MyStatic@printUrl()"/>
</body>
结果:

(3)OGNL 访问复杂对象
我们以javaBean对象为例:Student类
 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;
     }
 }
Success.jsp文件:
<html>
<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/>
<s:property value="students[2].name"/>
<s:property value="students[2].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>
HelloAction文件代码:
 public class HelloAction extends ActionSupport{
     private static final long serialVersionUID = 1L;
     private Student student;//javaBean
     private List<Student>students;//list
     private Map<String,Student>studentMap;//Map
     public Student getStudent() {
         return student;
     }
     public void setStudent(Student student) {
         this.student = student;
     }
     public List<Student> getStudents() {
         return students;
     }
     public void setStudents(List<Student> students) {
         this.students = students;
     }
     public Map<String, Student> getStudentMap() {
         return studentMap;
     }
     public void setStudentMap(Map<String, Student> studentMap) {
         this.studentMap = studentMap;
     }
     @Override
     public String execute() throws Exception {
         // TODO Auto-generated method stub
         students=new ArrayList<Student>();
         student=new Student("小八",12);
         students.add(new Student("小酒",13));
         students.add(new Student("小石",14));
         students.add(new Student("十一",15));
         studentMap=new HashMap<String,Student>();
         studentMap.put("goodStudent", new Student("学霸",20));
         studentMap.put("badStudent", new Student("学渣",19));
         return SUCCESS;
     }
 }
显示结果:

Struts2的值栈和OGNL牛逼啊的更多相关文章
- Struts2知识点小结(三)--值栈与ognl表达式
		1.问题一 : 什么是值栈 ValueStack 回顾web阶段 数据交互问题? 客户端提交数据 到 服务器端 request接受数据+BeanUtils实体封装 ... 
- 走进Struts2(五)— 值栈和OGNL
		值栈 1.值栈是什么? 简单说:就是相应每个请求对象的轻量级的内存数据中心. Struts2引入值栈最大的优点就是:在大多数情况下,用户根本无须关心值栈,无论它在哪里,不用管它里面有什么,仅仅须要去获 ... 
- struts2(二)值栈 threadlocal ogal ui
		值栈(重要)和ognl表达式 1. 只要是一个mvc框架,必须解决数据的存和取的问题 2. Struts2利用值栈来存数据,所以值栈是一个存储数据的内存结构 3. 把数据存在值栈中,在页面上利用 ... 
- 关于Struts2中的值栈与OGNL表达式
		1.1.1 OGNL概述: Object Graphic Navigation Language(对象图导航语言)的缩写 * EL :OGNL比EL功能强大很多倍. 它是一个开源项目. ... 
- Struts2基础学习(七)—值栈和OGNL
		目录: 一.值栈 二.OGNL表达式 一.值栈(ValueStack) 1.定义 ValueStack贯穿整个Acton的生命周期,每个Action类的对象实例都拥有一个ValueStack ... 
- Struts2学习记录-Value Stack(值栈)和OGNL表达式
		仅仅是学习记录.把我知道的都说出来 一.值栈的作用 记录处理当前请求的action的数据. 二,小样例 有两个action:Action1和Action2 Action1有两个属性:name和pass ... 
- Struts2 的 值栈和ActionContext
		1.ValueStack 和 ActionContext 的关系与区别: -- 相同点:它们都是在一次HTTP请求的范围内使用的,它们的生命周期都是一次请求 -- 不同点:ValueStack 分为对 ... 
- Struts2的值栈和对象栈
		ValueStack 如何得到值栈: 如何将对象存入值栈: 让值栈执行表达式来获得值: 在JSP中跳过栈顶元素直接访问第二层: 在JSP中访问值栈对象本身(而不是它们的属性) ActionContex ... 
- 值栈与ognl
		ValueStack (值栈): 1.贯穿整个Action的生命周期(每个Action类的对象实例都拥有一个ValueStack对象).相当于一个数据的中转站.在其中保存当前Action对象和其他相关 ... 
随机推荐
- Codeforces Round #271 (Div. 2)-B. Worms
			http://codeforces.com/problemset/problem/474/B B. Worms time limit per test 1 second memory limit pe ... 
- What is the difference between try/except and assert?
			assert only check if a condition is true or not and throw an exception. A try/except block can run a ... 
- lucene测试类
			package test.lucene; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStrea ... 
- vsftp配置日志及其启用本地时间
			vsftp配置日志及其启用本地时间 1. 启用vsftp日志 xferlog_enable=YES xferlog_std_format=YES xferlog_file=/var/log/xferl ... 
- vue-router介绍及简单使用
			一.vue-router介绍 vue-router是vue官方提供的一个路由框架,控制页面路由,使用较为方便. 1.路由模式 hash(浏览器环境默认值),使用 URL hash 值来作路由,支持所有 ... 
- noip_最后一遍_2-图论部分
			大体按照 数学 图论 dp 数据结构 这样的顺序 模板集 这个真的只有模板了……………… ·spfa #include<bits/stdc++.h> using namespace std ... 
- Ubuntu apt-get出现unable to locate package解决方案
			前言 刚安装好的ubuntu 17发现apt-get安装指令异常. 故经网上搜索调查发现,发现这个问题基本是因为apt-get需要更新的缘故. 解决方案 只需使用命令升级更新即可. sudo apt- ... 
- 【mysql】配置 选项文件
			在Windows中,MySQL程序从以下文件读取启动选项: 文件名 目的 WINDIR\my.ini 全局选项 C:\my.cnf 全局选项 INSTALLDIR\my.ini 全局选项 defaul ... 
- cf     1029  C
			C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ... 
- 如何在eclipse中引用第三方jar包
			在用UiAutomator做手机自动化测试过程中,在UiAutomator的基础之上进一步封装了里边的方法,以使case开发更顺手.直接在工程的根目录下新建了个libs的文件夹,把封装好的框架打成ja ... 
