一 .OGNL介绍
1.OGNL是Object-Graph Navigation Language的缩写(也称为对象图导航语言),它是一种功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性
2.OgnlContext(ongl上下文)其实就是Map (教室、老师、学生)
分为两个对象:根对象(Root)和非根对象
例子:
map 教室
OgnlContext=根对象(1)+非根对象(N)
老师:跟对象 1
学生:非根对象
非根对象要通过"#key"访问,根对象可以省略"#key"
老师是根对象,所以"#“可以不用
学生是非根对象,所以要使用"#"
strust2中ognl结构图:
重点:
1、一个上下文中只有一个根对象
2、取跟对象的值,只需要直接通过根对象属性即可
3、非根对象取值必须通过指定的上下文容器中的#key.属性去取。
二. ValueStac
1 . 值栈
先进后出的数据结构,弹夹 push/pop
2 .为什么要使用ValueStack作为根对象
放到值栈中的对象都可视为根对象
从小到大
page -> request -> session -> application
从上至下
A
B
C
D
三 .ActionContext
1 . ActionContext ac = ActionContext.getContext();//保证同一请求中只创建一个上下文
request
session
application
parameters
ValueStack(root)
2 . 向ValueStack压栈
push(XxxAction)//helloAction *action
push(ModelDirver.getModel())//model不为null user
3 . Map<String,String[]> map = request.getParamterMap(); 压action代码详解
//参数名==OGNL表达式
{“userName”:“aaa”,“uname”:“bbb”,“upwd”:“ccc”,“age”:“22”}
工具类:
Student:
package com.xhh.ognl;
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 + "]";
}
}
Employee:
package com.xhh.ognl;
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 + "]";
}
}
Manager:
package com.xhh.ognl;
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 + "]";
}
}
Address:
package com.xhh.ognl;
public class Address {
private String city;
private String country;
public Address() {
super();
}
public Address(String city, String country) {
super();
this.city = city;
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address [city=" + city + ", country=" + country + "]";
}
}
OnglExpression:
package com.xhh.ognl;
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);
}
}
}
Demo1:
package com.xhh.ognl;
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);//小明-->小芳
}
}
测试:
Demo7:
package com.xhh.ognl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
public class Demo7 {
/**
*
* 值栈的使用
*
*/
public String test1() {
// 栈:表示一个先进后出的数据结构
ValueStack vs = ActionContext.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";
}
/**
* 此例用于模拟struts2的值栈计算过程
*
* @param args
*/
public String test2() {
ValueStack vs = ActionContext.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
System.out.println(vs.findValue("salary2"));//
ActionContext ac = ActionContext.getContext();
return "rs";
}
}
struts-sy.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>
<!--
相对mvc的差异性
package:用来将一类子控制器进行分类
http://localhost:8080/stauts/sy/user_add.action
中/sy对应的namespace="/sy"
extends包的继承
*的含义:
代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,代表了adds
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.xhh.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
<action name="demo_*" class="com.xhh.web.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="tomcat_*" class="com.xhh.web.TomacatAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="ognl_*" class="com.xhh.ognl.Demo7" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="clz_*" class="com.xhh.web.ClazzAction" method="{1}">
<result name="list">/clzList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="chain">clz_list</result>
</action>
</package>
</struts>
测试: