Ognl学习资料

OGNL
1、 Ognl(Object Graph Navigation Language) 对象图导航语言
它是一种功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的
任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。
它使用相同的表达式去存取对象的属性。这样可以更好的取得数据。
OGNL 可以让我们用非常简单的表达式访问对象层,例如,当前环境的根对象为
house ,则表达式 street.district.districtName 可以访问到 house 的街道属性所在的
区域的
districtNmae 属性。
各个类
区域类:

**************************************************************************************

public class District {
private int id;
private String disname;
public District() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDisname() {
return disname;
}
public void setDisname(String disname) {
this.disname = disname;
}

}
**************************************************************************************
街道类:
**************************************************************************************
public class Street {
private int id;
private String streetName;
private District district;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public District getDistrict() {
return district;
}
public void setDistrict(District district) {

this.district = district;
}
public Street(int id, String streetName, District district) {
super();
this.id = id;
this.streetName = streetName;
this.district = district;
}
public Street() {
super();
}
}
**************************************************************************************
房屋类型类:
**************************************************************************************
public class HouseType {
private int id;
private String typeName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public HouseType(int id, String typeName) {
super();
this.id = id;
this.typeName = typeName;
}
public HouseType() {
super();
}
}
**************************************************************************************
房屋类
**************************************************************************************
public class House {
private int id;
private String title;
private HouseType houseType;
private Street street;
private double price;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public HouseType getHouseType() {
return houseType;
}
public void setHouseType(HouseType houseType) {
this.houseType = houseType;
}
public Street getStreet() {
return street;
}
public void setStreet(Street street) {
this.street = street;
}

public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public House(int id, String title, HouseType houseType, Street street,
double price) {
super();
this.id = id;
this.title = title;
this.houseType = houseType;
this.street = street;
this.price = price;
}
public House() {
super();
}
}
**************************************************************************************
OGNL
语法测试
&&&&&&&&&&&&&&&&&&&&&& OGNL 语法测试 &&&&&&&&&&&&&&&&&&&
@Test
public void test() throws OgnlException {

// 房屋类型
HouseType houseType1=new HouseType(1001," 一室一厅 ");
HouseType houseType2=new HouseType(1002,"
二室一厅 ");
//
区域
District district1=new District(1," 高新区 ");
District district2=new District(1,"
雁塔区 ");
//
街道
Street street1=new Street(1," 光华路 ",district1);
Street street2=new Street(2,"
科技路路 ",district2);
House house1=new House(1, "
一间好房 ", houseType1, street1, 1005.2);
House house2=new House(2, "
又一间好房 ", houseType2, street2, 1800.0);
//java
中的访问方式
System.out.println("java===>"+house1.getStreet().getDistrict().getDisname());
//
访问单对象
String expression="street.district.disname";
//String expression="#root.street.district.disname"; //
完整的语法
String disname= (String) Ognl.getValue(expression, house1);
System.out.println(disname);
//
访问多对象
Map<String,Object> context=new HashMap<String, Object>();
context.put("h1", house1);
context.put("h2", house2);
//
针对 key 来找
String expression1="#h2.street.district.disname";
//String expression1="#context.h2.street.district.disname";// 完整的语法
// context 集合 就是 context, house1 就是 root 对象, expression1 就是
ognl 表达式
String disname1= (String) Ognl.getValue(expression1, context, house1);
System.out.println("ognl=====>"+disname1);
//
也可以以根的形式来访问
String disname2=(String) Ognl.getValue("#root.street.district.disname",
context, house1);
System.out.println(disname2);
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
例如: Ognl 中有两块区域, root context Root 区域可以理解为栈,每次访问
时取的是栈顶的对象,
context 区域可以理解为栈的上下文。 其实 root 也是

context 中的内容, 只不过是单独拿出来放在 root 区域。


Struts 2:
OGNL 是 Struts2 默认的表达式语言,开源,功能更强大
OGNL 在 Struts2 中能做什么:
存取对象的属性、调用对象的方法;访问静态方法和属性
访问值栈以及 Stack Context(非值栈) 中的数据
操作集合对象
支持赋值、运算操作、字段类型转化等
【Ctrl+Shift+H】
Open Type in Hierarchy(在类继承层次窗口中打开指定的 Type)
通过<s:property value=“house.title”/>为什么可以获取到房屋的标题信息?
house 为 Action 的属性, Action 的实例放在值栈(Value Stack)中
OGNL 可以直接访问值栈中的值
值栈(Value Stack) root
存放数据的内存空间
值栈中存放了 Action 的实例
如:可以通过 OGNL 来访问 Action 实例中的属性值
为什么通过 <s:property value=“#session.user.usernmae” />可以取得
session 保存的值?
session 的相关属性以及参数存放在 Stack Context 中
OGNL 可以通过#访问 Stack Context 的值
Stack Context context
存放数据的内存空间, Stack Context 中存放了
request 的参数、属性
session 的属性、 application 的属性
attr:在所有的属性范围中获取值,依次搜索 page、 request、 session
和 application
OGNL 一般都是和 Struts2 标签一起使用
<s:debug/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值