OGNL Java

package com.dys.po;
public class Person { private String name; private Dog dog;
public Dog getDog() {   return dog; }
public void setDog(Dog dog) {   this.dog = dog; }
public String getName() {   return name; }
public void setName(String name) {   this.name = name; }
@Override public String toString() { // TODO Auto-generated method stub   return name; } } 
 
 
package com.dys.po;
public class Dog { private String name; private String[] friends;
public String[] getFriends() {   return friends; }
public void setFriends(String[] friends) {   this.friends = friends; }
public String getName() {   return name; }
public void setName(String name) {   this.name = name; }
} 
 
package com.dys.test;
import com.dys.po.Dog; 
import com.dys.po.Person; 
import java.util.*; 
import ognl.Ognl; 
import ognl.OgnlContext;
public class OGNLTest { 
public static void main(String[] args) throws Exception {
Person person=new Person();   
person.setName("zhangsan");   
Dog dog1=new Dog();   
dog1.setName("personWangcai");   
person.setDog(dog1);     
Dog dog =new Dog();   
dog.setName("wangcai");   
OgnlContext context=new OgnlContext();   
context.put("person", person);   
context.put("dog", dog);   
context.setRoot(person);     
Object name=Ognl.parseExpression("name");   
Object nameValue=Ognl.getValue(name, context, context.getRoot());   
System.out.println(nameValue);   
System.out.println("----------------------------------------------");   
Object name1=Ognl.parseExpression("#person.name");   
Object nameValue1=Ognl.getValue(name1, context, context.getRoot());   
System.out.println(nameValue1);   
System.out.println("----------------------------------------------");   
Object name2=Ognl.parseExpression("#dog.name");   
Object nameValue2=Ognl.getValue(name2, context, context.getRoot());   
System.out.println(nameValue2);   
System.out.println("----------------------------------------------");   
Object name3=Ognl.parseExpression("dog.name");   
Object nameValue3=Ognl.getValue(name3, context,context.getRoot());   
System.out.println(nameValue3);   
System.out.println("----------------------------------------------");   
Object name4=Ognl.parseExpression("#person.dog.name");   
Object nameValue4=Ognl.getValue(name4, context,context.getRoot());   
System.out.println(nameValue4);   
System.out.println("----------------------------------------------");   
Object name5=Ognl.parseExpression("name.toUpperCase().length()");   
Object nameValue5=Ognl.getValue(name5, context,context.getRoot());   
System.out.println(nameValue5);   
System.out.println("----------------------------------------------");   
Object name6=Ognl.parseExpression("@java.lang.Integer@toBinaryString(8)");   
Object nameValue6=Ognl.getValue(name6, context,context.getRoot());   
System.out.println(nameValue6);   
System.out.println("----------------------------------------------");   
Object name7=Ognl.parseExpression("@@abs(-80)");
//Math中的方法可以省略包名   
Object nameValue7=Ognl.getValue(name7, context,context.getRoot());   
System.out.println(nameValue7);   
System.out.println("----------------------------------------------");   
Object nameValue8=Ognl.getValue("new java.util.ArrayList()",context,context.getRoot());   
System.out.println(nameValue8);   
System.out.println("----------------------------------------------");   
Object nameValue9=Ognl.getValue("{'dog','cat','mouse','elephant'}", context, context.getRoot());   
System.out.println(nameValue9);   
System.out.println("----------------------------------------------");   
Object nameValue10=Ognl.getValue("{'dog','cat','mouse','elephant'}[2]", context, context.getRoot());   System.out.println(nameValue10);   
System.out.println("----------------------------------------------");   
dog.setFriends(new String[]{"blackdog","whitedog","orangdog"});   
Object nameValue11=Ognl.getValue("#dog.friends[2]", context,context.getRoot());   
System.out.println(nameValue11);   
System.out.println("----------------------------------------------");   
ArrayList<String> list=new ArrayList<String>();  
 list.add("one");   list.add("two");   list.add("three");   
list.add("four");   context.put("list", list);   
Object nameValue12=Ognl.getValue("#list", context,context.getRoot());   
System.out.println(nameValue12);   
Object nameValue13=Ognl.getValue("#list[2]", context,context.getRoot());   
System.out.println(nameValue13);   
System.out.println("----------------------------------------------");   
Object nameValue14=Ognl.getValue("#{'one':'壹','two':'贰','three':'叁'}",context,context.getRoot());   
System.out.println(nameValue14);   
Object nameValue15=Ognl.getValue("#{'one':'壹','two':'贰','three':'叁'}.keySet()",context,context.getRoot());   System.out.println(nameValue15);   
Object nameValue16=Ognl.getValue("#{'one':'壹','two':'贰','three':'叁'}.values()",context,context.getRoot());   System.out.println(nameValue16);   
Object nameValue17=Ognl.getValue("#{'one':'壹','two':'贰','three':'叁'}['two']",context,context.getRoot());   System.out.println(nameValue17);   
System.out.println("----------------------------------------------");   
LinkedList<Person> list1=new LinkedList<Person>();   
Person person1=new Person();   
person1.setName("lisi");   
Person person2=new Person();   
person2.setName("wangshipu");   
Person person3=new Person();   
person3.setName("baijuyi");   
list1.add(person1);   
list1.add(person2);   
list1.add(person3);   
context.put("list1", list1);   
//过滤   
Object nameValue18=Ognl.getValue("#list1.{?#this.name.length()>4}[0]", context,context.getRoot());
//size()==size 伪属性 isEmpty()==isEmpty   
System.out.println(nameValue18);   
System.out.println("----------------------------------------------");
Object nameValue19=Ognl.getValue("#list1.{^#this.name.length()>4}", context,context.getRoot());   System.out.println(nameValue19);   
System.out.println("----------------------------------------------");
System.out.println("----------------------------------------------");   
Object nameValue21=Ognl.getValue("#list1.{$#this.name.length()>4}",context,context.getRoot());   
System.out.println(nameValue21);
//Collection.{? express}过滤
//Collection.{^ express}取出第一个元素
//Collection.{$ express}取出最后一个元素
System.out.println("----------------------------------------------");   
Object nameValue22=Ognl.getValue("#list1.{name}", context,context.getRoot());   
System.out.println(nameValue22);   
System.out.println("----------------------------------------------");   
Object nameValue23=Ognl.getValue("#list1.{#this.name}", context,context.getRoot());   
System.out.println(nameValue23);   
System.out.println("----------------------------------------------");   
Object nameValue24=Ognl.getValue("#list1.{#this.name.length()>4?#this.name:'hello'}", context,context.getRoot());   System.out.println(nameValue24);
 
 
[N]语法,意思是取部分值栈,例如 [2]意思是取出从栈底到栈顶2的栈所有元素,而[2].top是取部分栈的最上面一个元素;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值