什么是ognl?
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性
OGNL在框架中主要做两件事情:
表达式语言和类型转换器
OGNL在框架中的作用以及数据的流入流出
OGNL是通常要结合Struts 2的标志一起使用。主要是#、%和$这三个符号的使用:
"#"符号有三种用途
1、使用 # 来获取Action值栈中Context部分中的数据,相当于ActionContext.getContext();
2、用于过滤和投影(projecting)集合,如books.{?#this.price<100};
3、构造Map,如#{‘foo1’:’bar1’, ‘foo2’:’bar2’}
1-1)parameters :包含当前HTTP请求参数的Map
#parameters.id[0]=>作用相当于request.getParameter("id")
1-2)request:包含当前HttpServletRequest的属性(attribute)的Map
#request.userName=>作相当于于request.getAttribute("userName");
1-3)session:包含当前HttpSession属性(attribute)的Map
#session.userName=>session.getAttribute("userName");
1-4)application:包含当前应用的ServletContext的属性(attribute)的Map
#application.userName=>application.getAttribute("userName");
"%"符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值
"$"有两个主要的用途,用于在国际化资源文件中,引用OGNL表达式。在Struts 2和i18n中配置文件
什么是值栈?
值栈就是框架创建的一个存储区域,用来保存Model对象,它具有栈的特性,可以存放多个对象,但是要按照先后顺序压入栈的。框架处理每个请求时,都会创建对应的运行环境,这是会创建值栈和请求对应的Action 实例,并将每个Action实例压入值栈中。值栈是一个虚拟对象,它可以暴露它所包含的所有对象的的属性,就好像这些属性是它自己的一样。
所以
OGNL访问值栈时从它的上往下访问不管值栈是否有关重复的靠近值栈最上面的属性都会被优先使用
代码实际
首先
导入ognl的jar包:ognl-3.0.6.jar,Struts的核心包
Struts2的标签库:<%@ taglib prefix=“s” uri = “/struts-tags” %>
OnglExpression 核心类:
package com.Tang.test;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
/**
* 用于OGNL表达计算的一个工具类
*/
public class OnglExpression {
private OnglExpression() {
}
/**
* 根据OGNL表达式进行取值操作
* @param expression
* ognl表达式
* @param ctx
* ognl上下文
* @param rootObject
* ognl根对象
* @return
*/
public static Object getValue(String expression, OgnlContext ctx,
Object rootObject) {
try {
return Ognl.getValue(expression, ctx, rootObject);
} catch (OgnlException e) {
throw new RuntimeException(e);
}
}
/**
* 根据OGNL表达式进行赋值操作
* @param expression
* ognl表达式
* @param ctx
* ognl上下文
* @param rootObject
* ognl根对象
* @param value
* 值对象
*/
public static void setValue(String expression, OgnlContext ctx,
Object rootObject, Object value) {
try {
Ognl.setValue(expression, ctx, rootObject, value);
} catch (OgnlException e) {
throw new RuntimeException(e);
}
}
}
Manager.java(实体类):
package com.Tang.test;
public class Manager {
private String name;
public Manager() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Manager [name=" + name + "]";
}
}
Employee.java (实体类):
package com.Tang.test;
public class Employee {
private String name;
private Address address;
private Integer salary;
public Employee() {
super();
}
public Employee(String name, Integer salary) {
super();
this.name = name;
this.salary = salary;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Employee [name=" + name + ", address=" + address + ", salary=" + salary + "]";
}
}
Student.java (实体类);
package com.Tang.test;
public class Student {
private String name;
private String number;
public Student() {
super();
}
public Student(String name, String number) {
super();
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Student [name=" + name + ", number=" + number + "]";
}
}
实例1
package com.Tang.test;
import ognl.OgnlContext;
import ognl.OgnlException;
public class Demo1 {
/**
* @param args
* @throws OgnlException
*/
public static void main(String[] args) {
//小李 员工
Employee e = new Employee();
e.setName("小李");
//管理人员 张经理
Manager m = new Manager();
m.setName("张经理");
// 创建OGNL下文,而OGNL上下文实际上就是一个Map对象
OgnlContext ctx = new OgnlContext();
// 将员工和经理放到OGNL上下文当中去
ctx.put("employee", e);
ctx.put("manager", m);
ctx.setRoot(e);// 设置OGNL上下文的根对象
/** ********************** 取值操作 *************************** */
// 表达式name将执行e.getName(),因为e对象是根对象(请注意根对象和非根对象表达式的区别)
String employeeName = (String) OnglExpression.getValue("name", ctx, e);
System.out.println(employeeName);//小李
// 表达式#manager.name将执行m.getName(),注意:如果访问的不是根对象那么必须在前面加上一个名称空间,例如:#manager.name
String managerName = (String) OnglExpression.getValue("#manager.name",
ctx, e);
System.out.println(managerName);//张经理
// 当然根对象也可以使用#employee.name表达式进行访问
employeeName = (String) OnglExpression.getValue("#employee.name", ctx,
e);
System.out.println(employeeName);//小李
/** ********************** 赋值操作 *************************** */
OnglExpression.setValue("name", ctx, e, "小明");
employeeName = (String) OnglExpression.getValue("name", ctx, e);
System.out.println(employeeName);//小明
OnglExpression.setValue("#manager.name", ctx, e, "孙经理");
managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);
System.out.println(managerName);//孙经理
OnglExpression.setValue("#employee.name", ctx, e, "小芳");
employeeName = (String) OnglExpression.getValue("name", ctx, e);
System.out.println(employeeName);//小芳
}
}
输出结果
实例2(证实他的顺序)
package com.Tang.test;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
public class DemoAction {
public String test1() {
//ValueStack是一個堆栈结构的容器 特点:先进后出
ValueStack vs = ServletActionContext.getContext().getValueStack();
vs.push(new Employee("张雇员", 2000));// 1
vs.push(new Student("小明同学", "s001"));// 0
System.out.println(vs.findValue("name"));//小明
System.out.println(vs.findValue("salary"));//2000
return "rs";
}
public String test2() {
// 栈:表示一个先进后出的数据结构
ValueStack vs = ServletActionContext.getContext().getValueStack();
// push方法把项压入栈顶
vs.push(new Employee("zs", 22));
vs.push(new Employee("ls", 22));
vs.push(new Employee("ww", 22));
// pop方法移除栈顶对象并作为此函数的值返回该对象
Employee e = (Employee) vs.pop();
System.out.println(e.getName());
e = (Employee) vs.pop();
System.out.println(e.getName());
e = (Employee) vs.pop();
System.out.println(e.getName());
return "rs";
}
}
配置xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<action name="/demo_*" class="com.Tang.web.HelloAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="/stack_*" class="com.Tang.test.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
</package>
</struts>
显示界面jsp加入
<a href="${pageContext.request.contextPath }/sy/stack_test1.action">ognl</a>
test1测试结果
test2测试结果
结论:值栈取值从上往下,取到为止,如果已经拿到,不再往下找