OGNL以及存取实列

对象—视图导航语言(类似EL表达式)
前后台数据传输
OgnlContext:核心类
1.ROOT:
只允许放pojo对象
setRoot(Object obj)
2.context
只允许存放map
SetValues(Map map)
2.存放数据
创建ognl对象
创建一个java类
3.创建数据
oc .setRoot(user)
oc.setValues(map1)

public class OnglTest {
    
    
private static OgnlContext oc = new OgnlContext();
public static void putDataToOgnlContext() {
    
    
// 1. 放置root
User user = new User("小明",18);
oc.setRoot(user);
// 2. 放置context
Map<String,User> userMap = new HashMap<String,User>();
userMap.put("user1", new User("小红",22));
userMap.put("user2", new User("小强",19));
oc.setValues(userMap);
}

public static void getDataFromOgnlContext() throws OgnlException {
    
    
//1.取root
String name = (String)Ognl.getValue("name", oc, oc.getRoot());
System.out.println("root->name:" + name);
String contextUser1Name = (String)Ognl.getValue("#user1.name", oc , oc.getRoot());
System.out.println("context -> user1 -> name:" + contextUser1Name);
}
//1.取context
public static void main(String[] args) throws OgnlException {
    
    
putDataToOgnlContext(); // 准备工作 --- root 和 context 的生成
getDataFromOgnlContext(); // 最简单的语法 - 取值
}
}

运行结果:
root->name:xiao hong
context -> user1 -> name:xiao lan 

猜你喜欢

转载自blog.csdn.net/weixin_44703894/article/details/111562649