OGNL and access real column

Object-view navigation language (similar to EL expression)
front and back data transmission
OgnlContext: core class
1. ROOT:
only allow pojo object
setRoot (Object obj)
2. context
only allow to store map
SetValues ​​(Map map)
2. Store data
creation ognl object
Create a java class
3. Create data
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 

Guess you like

Origin blog.csdn.net/weixin_44703894/article/details/111562649