📄 ognltest.java
字号:
package com.bjsxt.crm.ognl.test;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import ognl.Ognl;
import com.bjsxt.crm.model.User;
public class OgnlTest extends TestCase {
//
public void testOgnl_01(){
try {
Map context = new HashMap();
User user = new User();
context.put("user", user);
//赋值:ognl表达式、context、root Object、value
Ognl.setValue("#user.username", context, null,"你好");
//取值:ognl表达式、context、rootObject
Object value = Ognl.getValue("#user.username", context,new Object());
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
public void testOgnl_02(){
try {
Map context = new HashMap();
User user = new User();
//赋值:ognl表达式、context、root Object、value
Ognl.setValue("username", context, user,"你好");
//取值:ognl表达式、context、rootObject
Object value = Ognl.getValue("username", context,user);
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
public void testOgnl_03(){
try {
Map context = new HashMap();
Object root = new Object();
//取值:ognl表达式、context、rootObject
Object value = Ognl.getValue("new int[]{1,2,3}", context,root);
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
public void testOgnl_04(){
try {
Map context = new HashMap();
Object root = new Object();
//取值:ognl表达式、context、rootObject
//调用静态方法
Object value = Ognl.getValue("@com.bjsxt.crm.ognl.test.JustAObject@hello(18)", context,root);
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
public void testOgnl_05(){
try {
Map context = new HashMap();
Object root = new Object();
//取值:ognl表达式、context、rootObject
//创建对象,并调用方法
Object value = Ognl.getValue("new com.bjsxt.crm.ognl.test.JustAObject().hello1()", context,root);
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -