所有的学习我们必须先搭建好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 顺序查找值

例子:

  1. struts.xml
    1 <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5.  
  6. <struts>
  7.  
  8. <package name="manage" namespace="/" extends="struts-default">
  9.  
  10. <action name="hello" class="com.wishwzp.action.HelloAction">
  11. <result name="success" >success.jsp</result>
  12. </action>
  13. </package>
  14.  
  15. </struts>
  1. HelloAction.java
    1 package com.wishwzp.action;
  2.  
  3. import java.util.Map;
  4.  
  5. import com.opensymphony.xwork2.ActionContext;
  6. import com.opensymphony.xwork2.ActionSupport;
  7. import com.opensymphony.xwork2.util.ValueStack;
  8.  
  9. public class HelloAction extends ActionSupport{
  10.  
  11. /**
  12. *
  13. */
  14. private static final long serialVersionUID = 1L;
  15.  
  16. @Override
  17. public String execute() throws Exception {
  18. // 获取ActionContext
  19. ActionContext actionContext=ActionContext.getContext();
  20. // 获取狭义上的值栈
  21. // 值栈是用来存储数据的
  22. ValueStack valueStack=actionContext.getValueStack();
  23. valueStack.set("name", "张三(valueStack)");
  24. valueStack.set("age", 11);
  25.  
  26. Map<String, Object> session=actionContext.getSession();
  27. session.put("name", "王五(session)");
  28. session.put("age", 13);
  29.  
  30. Map<String, Object> application=actionContext.getApplication();
  31. application.put("name", "赵六(application)");
  32. application.put("age", 14);
  33. return SUCCESS;
  34. }
  35. }
  1. success.jsp
    1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib prefix="s" uri="/struts-tags" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. <%
  10. request.setAttribute("name", "李四(request)");
  11. request.setAttribute("age", "12");
  12. %>
  13. </head>
  14. <body>
  15. 获取狭义上的值栈数据:<s:property value="name"/>
  16. <s:property value="age"/><br/>
  17. 请求参数:<s:property value="#parameters.name"/>
  18. <s:property value="#parameters.age"/><br/>
  19. request:<s:property value="#request.name"/>
  20. <s:property value="#request.age"/><br/>
  21. session:<s:property value="#session.name"/>
  22. <s:property value="#session.age"/><br/>
  23. application:<s:property value="#application.name"/>
  24. <s:property value="#application.age"/><br/>
  25. attr取值:<s:property value="#attr.name"/>
  26. <s:property value="#attr.age"/><br/>
  27. </body>
  28. </html>

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

结果:

第三节:OGNL 访问复杂对象

1,访问javaBean 对象;

2,访问集合对象;

3,访问Map 对象;

例子:

  1. struts.xml
    1 <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5.  
  6. <struts>
  7.  
  8. <package name="manage" namespace="/" extends="struts-default">
  9.  
  10. <action name="hello" class="com.wishwzp.action.HelloAction">
  11. <result name="success" >success.jsp</result>
  12. </action>
  13. </package>
  14.  
  15. </struts>
  1. Student.java
    1 package com.wishwzp.model;
  2.  
  3. public class Student {
  4.  
  5. private String name;
  6. private int age;
  7.  
  8. public Student() {
  9. super();
  10. // TODO Auto-generated constructor stub
  11. }
  12. public Student(String name, int age) {
  13. super();
  14. this.name = name;
  15. this.age = age;
  16. }
  17.  
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public int getAge() {
  25. return age;
  26. }
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30.  
  31. }
  1. HelloAction.java
    1 package com.wishwzp.action;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import com.wishwzp.model.Student;
  9. import com.opensymphony.xwork2.ActionContext;
  10. import com.opensymphony.xwork2.ActionSupport;
  11. import com.opensymphony.xwork2.util.ValueStack;
  12.  
  13. public class HelloAction extends ActionSupport{
  14.  
  15. /**
  16. *
  17. */
  18. private static final long serialVersionUID = 1L;
  19.  
  20. //访问javaBean对象,并get...和set...
  21. private Student student;
  22.  
  23. //访问集合对象,并get...和set...
  24. private List<Student> students;
  25.  
  26. //访问Map对象,并get...和set...
  27. private Map<String,Student> studentMap;
  28.  
  29. public Map<String, Student> getStudentMap() {
  30. return studentMap;
  31. }
  32.  
  33. public void setStudentMap(Map<String, Student> studentMap) {
  34. this.studentMap = studentMap;
  35. }
  36.  
  37. public List<Student> getStudents() {
  38. return students;
  39. }
  40.  
  41. public void setStudents(List<Student> students) {
  42. this.students = students;
  43. }
  44.  
  45. public Student getStudent() {
  46. return student;
  47. }
  48.  
  49. public void setStudent(Student student) {
  50. this.student = student;
  51. }
  52.  
  53. @Override
  54. public String execute() throws Exception {
  55.  
  56. //javaBean对象
  57. student=new Student("小扒", 12);
  58.  
  59. //集合对象
  60. students=new ArrayList<Student>();
  61. students.add(new Student("老九",13));
  62. students.add(new Student("老十",14));
  63.  
  64. //Map对象
  65. studentMap=new HashMap<String,Student>();
  66. studentMap.put("goodStudent", new Student("学霸",20));
  67. studentMap.put("badStudent", new Student("学渣",19));
  68. return SUCCESS;
  69. }
  70.  
  71. }
  1. success.jsp
    1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib prefix="s" uri="/struts-tags" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. ognl访问javaBean对象:<s:property value="student.name"/>
  12. <s:property value="student.age"/><br/>
  13.  
  14. ognl访问List集合:<s:property value="students[0].name"/>
  15. <s:property value="students[0].age"/><br/>
  16. <s:property value="students[1].name"/>
  17. <s:property value="students[1].age"/><br/>
  18.  
  19. ognl访问Map:<s:property value="studentMap['goodStudent'].name"/>
  20. <s:property value="studentMap['goodStudent'].age"/><br/>
  21. <s:property value="studentMap['badStudent'].name"/>
  22. <s:property value="studentMap['badStudent'].age"/><br/>
  23. </body>
  24. </html>

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

结果:

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

1,访问静态属性;

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

例子:

  1. struts.xml
    1 <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5.  
  6. <struts>
  7.  
  8. <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
  9.  
  10. <package name="manage" namespace="/" extends="struts-default">
  11.  
  12. </package>
  13.  
  14. </struts>
  1. MyStatic.java
    1 package com.wishwzp.common;
  2.  
  3. public class MyStatic {
  4.  
  5. //静态属性
  6. public static final String str="Struts2开心学习";
  7.  
  8. //静态方法
  9. public static String printUrl(){
  10. System.out.println("http://www.baidu.com");
  11. return "http://www.baidu.com";
  12. }
  13. }
  1. ognl_static.jsp
    1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib prefix="s" uri="/struts-tags" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. 访问静态属性: <s:property value="@com.wishwzp.common.MyStatic@str"/><br/>
  12. 访问静态方法:<s:property value="@com.wishwzp.common.MyStatic@printUrl()"/>
  13. </body>
  14. </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. Entity Framework 学习初级篇1--EF基本概况

    转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685011.html 最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不 ...

  2. [转]Oracle查询树形数据的叶节点和子节点

    oracle 9i判断是叶子或根节点,是比较麻烦的一件事情,SQL演示脚本如下: --表结构-- DROP TABLE idb_hierarchical; create TABLE idb_hiera ...

  3. wojilu中的路由

    要看2个地方,一个是route.config,另一个是wojilu.Members.Sites.Domain.SiteMenu.config,这2部分综合起作用.

  4. 全新ASP框架——IISNODE介绍

    Asp是一门经典的动态网页编程语言,通常使用vbscript或者Jscript脚本来实现.一个好的框架,可以帮助您更加快速地使用Asp来完成您的网站开发任务.而Asp框架的终结者——IISNODE框架 ...

  5. ARM学习笔记5——程序状态寄存器

    当前程序状态寄存器CPSR可以在任何位处理器模式下被访问,它包含条件码标志.中断控制.当前处理器模式以及其他状态和控制信息.CPSR的结构图如下: 一.条件标志位 CPSR最高4位:N(Negativ ...

  6. win8下在microsoft visual studio 2012利用ODP.NET连接ORACLE 12c

    老板要求我搭个ASP.NET框架,并且连接上ORACLE数据库,听起来好像挺简单的,但就是连第一步连接ORACLE我都搞了两天╮(╯▽╰)╭ 首先,项目书上要求用ORACLE 10G,可我自己的本本装 ...

  7. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

  8. SpringMVC 流程 配置 接口

    SpringMVC简介    一 流程介绍 1.角色划分 前端控制器(DispatcherServlet).请求到处理器映射(HandlerMapping).处理器适配器(HandlerAdapter ...

  9. 微信小程序开发体验

    1.  申请小程序账号 小程序目前不支持个人申请,企业申请后填写基本信息 本来以为用原来公司申请的公众号就可以申请小程序权限,貌似不行 2.  添加开发者 管理员默认拥有开发者所有权限 添加其他开发者 ...

  10. 【转】Android仿QQ截图应用测试

    使用过QQ的同学应该都用过QQ截图,Ctrl+Alt+A进入截图操作,通过拉伸,移动高亮区域的框体可以快速截取我们需要的图片.在android应用中,我们也经常需要截图操作,以下实现了一个类似QQ截图 ...