1.OGNL简介
- OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言。
OGNL在很多框架中作用工具使用,例如struts
2.OgnlContext
- OgnlContext(ongl上下文)其实就是Map<String,Object>。OgnlContext=根对象(1)+非根对象(N)。
非根对象要通过"#key"访问,根对象可以省略"#key"。
示例:
- 先创建一个maven项目 配置strust 以及导入需要依赖的jar包(详细看作者上一篇文章有详细教程)
- 创建Employee及Manager,用于演示
public class Employee {
private String name;
private Address address;
private Integer salary;
//自己生成getter,setter方法。。。
}
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 + "]";
}
}
- OnglExpression类,该类为工具类,用于简化ongl操作(需要自己编写)
/**
* 用于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);
}
}
}
- 用于示例:
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);
}
}
3. ValueStack
1)什么时候栈结构:
先进后出的数据结构,弹夹 push/pop。
2)为什么要使用ValueStack作为根对象
放到值栈中的对象都可视为根对象

4. struts基本工作流程
1) 流程示意图
- 伪代码
*.action
- ActionContext
3.1 ActionContext ac = ActionContext.getContext();//保证同一请求中只创建一个上下文
request
session
application
parameters
ValueStack(root)
3.2 向ValueStack压栈
ValueStack vs = ac.getValueStack();
vs.push(XxxAction)//helloAction
vs.push(ModelDirver.getModel())//model不为null user
3.3 Map<String,String[]> map = request.getParamterMap();
//参数名==OGNL表达式
{"userName":"aaa","uname":"kfc","upwd":"999","age":"17"}
//ognl赋值
setValue("userName", ac, vs, "zzz");
setValue("uname", ac, vs, "kfc");
setValue("upwd", ac, vs, "999");
setValue("age", ac, vs, "17");
user [0]
helloAction [1]
5. struts2中传递数据
- 可以使用作用域,但更多的是利用ValueStack或ActionContext
示例:
/*
* 1. 在struts中Action可以继续ActionSupport, 但不是必须的,ActionSupport中有
* 一些公用方法。
* 2. Action中的方法,返回String,方法不能带参数(这一点与自定义MVC不同,更方便)
* 3. 方法名可任意,struts默认调用execute方法。
* 4. Struts中Action是多例的
*/
public class TestAction extends ActionSupport implements ModelDriven<Student>{
private Student student = new Student();
//该属性与student中的属性重名,优先为student中的属性赋值
private String sname;
//存放需要传送到页面上的数据
private Object result;
public Object getResult() {
return result;
}
@Override
public Student getModel() {
return student;
}
public String hello() {
System.out.println("action do ........ ");
System.out.println(student);
System.out.println(sname);
this.result = "result object";
return "testPage";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>struts test .... </h1>
<h1>EL表达式获取值</h1>
<h1>${result}</h1>
<h1>Struts标签获取值</h1>
<h1><s:property value="result"/></h1>
</body>
</html>