OGNL在Struts2中的应用

在上篇讲述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中的其它对象,则必须添加"#"标识符,看例子:

package com.ognl.domain;

import java.io.Serializable;

public class Student implements Serializable{
	
	private static final long serialVersionUID = 6412121683349841035L;

	private String name;
	
	private int age;
	
	private double height;

	public Student() {
		super();
	}

	public Student(String name, int age, double height) {
		super();
		this.name = name;
		this.age = age;
		this.height = height;
	}

	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;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", height=" + height + "]";
	}
}

package com.ognl.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.ognl.domain.Student;
import com.opensymphony.xwork2.ActionSupport;

public class OgnlContextActon extends ActionSupport implements RequestAware, SessionAware, ApplicationAware {
	
	private List<Student> students;
	
	private Map<String,Object> requestMap;
	
	private Map<String,Object> applicationMap;
	
	private Map<String,Object> sessionMap;
	
	@Override
	public String execute() throws Exception {
		
		Student s1 = new Student("Tom", 22, 170.3);
		Student s2 = new Student("Jack", 21, 176.2);
		Student s3 = new Student("Tomas", 23, 180.1);
		Student s4 = new Student("Lucy", 20, 163.3);
		// 给Action的students属性赋值
		students = new ArrayList<Student>();
		Collections.addAll(students, s1, s2, s3, s4);
		// 放置到请求中
		requestMap.put("students", students);
		// 放置到session
		sessionMap.put("students",students);
		// 放置到application
		applicationMap.put("students", students);
		
		return SUCCESS;
	}
	
	@Override
	public void setApplication(Map<String, Object> arg0) {
		this.applicationMap = arg0;
	}
	
	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.requestMap = arg0;
	}
	
	@Override
	public void setSession(Map<String, Object> arg0) {
		this.sessionMap = arg0;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}
	
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <s:property value="students[0].name"/> or <s:property value="#request.students[0].name"/> or <s:property value="#session.students[0].name"/> or <s:property value="#application.students[0].name"/>
    <hr>
    <h1>获取并遍历Action中的students属性</h1>
    <s:iterator value="students" var="stu">
    	学生姓名为:<s:property value="name"/>, 年龄为:<s:property value="age"/> , 身高为:<s:property value="height"/><br>
    </s:iterator>
    <hr>
    <h1>获取并遍历request中的students属性</h1>
    <s:iterator value="#request.students" var="stu">
    	学生姓名为:<s:property value="name"/>, 年龄为:<s:property value="age"/> , 身高为:<s:property value="height"/><br>
    </s:iterator>
    <hr>
    <h1>获取并遍历session中的students属性</h1>
    <s:iterator value="#session.students" var="stu">
    	学生姓名为:<s:property value="name"/>, 年龄为:<s:property value="age"/> , 身高为:<s:property value="height"/><br>
    </s:iterator>
    <hr>
    <h1>获取并遍历application中的students属性</h1>
    <s:iterator value="#application.students" var="stu">
    	学生姓名为:<s:property value="name"/>, 年龄为:<s:property value="age"/> , 身高为:<s:property value="height"/><br>
    </s:iterator>
  </body>
</html>
只有正确理解了ActionContext以及OgnlContext的关系,以及上下文中存在哪些对象,以及根对象的构成,才能够在Struts2前后台交互传值中做到收缩自如,另外需要注意的是Struts2中OGNL表达式,必须与Struts2标签库搭配使用,否则不会有任何效果;


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值