OGNL在Struts2中的应用 ——OGNL学习(2)

 

在上篇讲述OGNL基础知识的博客中,我们可以得到如下知识点:

1、OGNL表达式需要放置到OgnlContext中才能得到正确地解析、解释和执行;

2、OgnlContext类实现了Map接口,所以可以使用put方法向里面放置元素且每个OgnlContext有且最多只能有一个根对象;

3、Ognl表达式中访问根对象的属性时,无需"#",普通对象则需要;

上 面知识了解后,咱们再来聊聊Struts2的ActionContext和OgnlContext的关系;ActionContext是一个Action 对象执行所依赖

的上下文环境,每一个Action在执行时都会有一个专属的上下文环境,这个上下文环境可以看作是装载如Action执行前后所需要

的比 如:application、session、request、parameters、attr等包括该Action对象在内的一个容器对象,既然这个 ActionContext

这么重要,到底是由谁担任这份工作呢?对,Struts2框架将OGNL Context设置为ActionContext,正是因为这个原因,我们可以放心

大胆地在Struts2中使用OGNL,那么我们看一下在这个 ActionContext中到底都有哪些对象:


第一眼感觉就是原来 Struts2把Servlet对象都封装在ActionContext里面了,依次介绍下,application代表ServletContext对 象,session代表HttpSession,request代表HttpServletRequest,parameters代表存储在HttpServletRequest 对象中的各种参数,attr代表我们通过setAttribute方法设置到各个对象(比如session、request)中的属性等,action代 表当前action对象,需要记住的是由于这些对象都是普通对象,所以我们如果想要访问这些对象属性的时候,需要用"#",比 如#session、#request、#parameters,那么哪个对象是ActionContext中的根对象呢?Struts2将value stack对象设置为根对象,每一个Action实例对象都会被放置到这个value stack,由于Action在Stack里面,且这个value stack是OGNL的根对象,所以当我们访问Action属性的时候,就可以省略到"#"标识符,但如果想要访问ActionContext中的其它对 象,则必须添加"#"标识符,看例子:

  1. package com.ognl.domain;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Student implements Serializable{  
  6.       
  7.     private static final long serialVersionUID = 6412121683349841035L;  
  8.   
  9.     private String name;  
  10.       
  11.     private int age;  
  12.       
  13.     private double height;  
  14.   
  15.     public Student() {  
  16.         super();  
  17.     }  
  18.   
  19.     public Student(String name, int age, double height) {  
  20.         super();  
  21.         this.name = name;  
  22.         this.age = age;  
  23.         this.height = height;  
  24.     }  
  25.   
  26.     public String getName() {  
  27.         return name;  
  28.     }  
  29.   
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.   
  34.     public int getAge() {  
  35.         return age;  
  36.     }  
  37.   
  38.     public void setAge(int age) {  
  39.         this.age = age;  
  40.     }  
  41.   
  42.     public double getHeight() {  
  43.         return height;  
  44.     }  
  45.   
  46.     public void setHeight(double height) {  
  47.         this.height = height;  
  48.     }  
  49.   
  50.     @Override  
  51.     public String toString() {  
  52.         return "Student [name=" + name + ", age=" + age + ", height=" + height + "]";  
  53.     }  
  54. }  
  1. package com.ognl.action;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.struts2.interceptor.ApplicationAware;  
  9. import org.apache.struts2.interceptor.RequestAware;  
  10. import org.apache.struts2.interceptor.SessionAware;  
  11.   
  12. import com.ognl.domain.Student;  
  13. import com.opensymphony.xwork2.ActionSupport;  
  14.   
  15. public class OgnlContextActon extends ActionSupport implements RequestAware, SessionAware, ApplicationAware {  
  16.       
  17.     private List<Student> students;  
  18.       
  19.     private Map<String,Object> requestMap;  
  20.       
  21.     private Map<String,Object> applicationMap;  
  22.       
  23.     private Map<String,Object> sessionMap;  
  24.       
  25.     @Override  
  26.     public String execute() throws Exception {  
  27.           
  28.         Student s1 = new Student("Tom", 22, 170.3);  
  29.         Student s2 = new Student("Jack", 21, 176.2);  
  30.         Student s3 = new Student("Tomas", 23, 180.1);  
  31.         Student s4 = new Student("Lucy", 20, 163.3);  
  32.         // 给Action的students属性赋值  
  33.         students = new ArrayList<Student>();  
  34.         Collections.addAll(students, s1, s2, s3, s4);  
  35.         // 放置到请求中  
  36.         requestMap.put("students", students);  
  37.         // 放置到session  
  38.         sessionMap.put("students",students);  
  39.         // 放置到application  
  40.         applicationMap.put("students", students);  
  41.           
  42.         return SUCCESS;  
  43.     }  
  44.       
  45.     @Override  
  46.     public void setApplication(Map<String, Object> arg0) {  
  47.         this.applicationMap = arg0;  
  48.     }  
  49.       
  50.     @Override  
  51.     public void setRequest(Map<String, Object> arg0) {  
  52.         this.requestMap = arg0;  
  53.     }  
  54.       
  55.     @Override  
  56.     public void setSession(Map<String, Object> arg0) {  
  57.         this.sessionMap = arg0;  
  58.     }  
  59.   
  60.     public List<Student> getStudents() {  
  61.         return students;  
  62.     }  
  63.   
  64.     public void setStudents(List<Student> students) {  
  65.         this.students = students;  
  66.     }  
  67.       
  68. }  
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head>  
  7.       
  8.     <title>My JSP 'index.jsp' starting page</title>  
  9.     <meta http-equiv="pragma" content="no-cache">  
  10.     <meta http-equiv="cache-control" content="no-cache">  
  11.     <meta http-equiv="expires" content="0">      
  12.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  13.     <meta http-equiv="description" content="This is my page">  
  14.     <!-- 
  15.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  16.     -->  
  17.   </head>  
  18.     
  19.   <body>  
  20.     <s:property value="students[0].name"/> or <s:property value="#request.students[0].name"/> or <s:property value="#session.students[0].name"/> or 
  21. <s:property value="#application.students[0].name"/>  
  22.     <hr>  
  23.     <h1>获取并遍历Action中的students属性</h1>  
  24.     <s:iterator value="students" var="stu">  
  25.         学生姓名为:<s:property value="name"/>, 年龄为:<s:property value="age"/> , 身高为:<s:property value="height"/><br>  
  26.     </s:iterator>  
  27.     <hr>  
  28.     <h1>获取并遍历request中的students属性</h1>  
  29.     <s:iterator value="#request.students" var="stu">  
  30.         学生姓名为:<s:property value="name"/>, 年龄为:<s:property value="age"/> , 身高为:<s:property value="height"/><br>  
  31.     </s:iterator>  
  32.     <hr>  
  33.     <h1>获取并遍历session中的students属性</h1>  
  34.     <s:iterator value="#session.students" var="stu">  
  35.         学生姓名为:<s:property value="name"/>, 年龄为:<s:property value="age"/> , 身高为:<s:property value="height"/><br>  
  36.     </s:iterator>  
  37.     <hr>  
  38.     <h1>获取并遍历application中的students属性</h1>  
  39.     <s:iterator value="#application.students" var="stu">  
  40.         学生姓名为:<s:property value="name"/>, 年龄为:<s:property value="age"/> , 身高为:<s:property value="height"/><br>  
  41.     </s:iterator>  
  42.   </body>  
  43. </html>  

 

只有正确理解了ActionContext以及OgnlContext的关系,以及上下文中存在哪些对象,以及根对象的构成,才能够在Struts2前后台 交互传值中做到收缩自如,另外需要注意的是Struts2中OGNL表达式,必须与Struts2标签库搭配使用,否则不会有任何效果;

 

posted @ 2017-10-24 16:05  Allen101  阅读(96)  评论(0)    收藏  举报