ognl-インタフェースデータバインド
5789 ワード
これは必要なときにまた研究しましょう...
import java.util.*;
import ognl.Ognl;
import ognl.OgnlException;
public class OgnlTest {
public static void main(String[] args) {
User user = new User();
user.setId(1);
user.setUsername("hello");
user.setPassword("nihao");
try {
Object o1 = Ognl.getValue("id", user);
Object o2 = Ognl.getValue("username", user);
Object o3 = Ognl.getValue("password", user);
System.out.println(o1 + "-" + o2 + "-" + o3);
// 、
System.out.println("// 、 ");
System.out.println(Ognl.getValue("str", user));
// 6、
System.out.println("// 6、 ");
System.out.println(Ognl.getValue(
"@entity.User_test@say(username+'<OK>')+',nihao'", user));//
// 7、
System.out.println("// 7、 ");
Dept dept = new Dept();
dept.setDeptName("Java ");
Emp e1 = new Emp("Tom", 3000.00, dept);
Emp e2 = new Emp("Nick", 7000.00, dept);
Emp e3 = new Emp("Jack", 4000.00, dept);
Emp e4 = new Emp("Mike", 12000.00, dept);
List<Emp> emps = new ArrayList<Emp>();
emps.add(e1);
emps.add(e2);
emps.add(e3);
emps.add(e4);
dept.setEmps(emps);
System.out.println(Ognl.getValue("dept", e1));
System.out.println(Ognl.getValue("dept.deptName", e1));
System.out.println(Ognl.getValue("emps[0].name", dept));
// 8、
System.out.println("// 8、 ");
Object o = Ognl.getValue("{1,2,3}", null);
System.out.println(o);
System.out.println(o.getClass().getName());//
// 9、 Map
System.out.println("// 9、 Map");
Object obj = Ognl.getValue("#{'mm':'MM','nn':'NN'}", null);
// ‘#’
System.out.println(obj);
System.out.println(obj.getClass().getName());
// 10、
System.out.println("// 10、 ");
Map<String, String> ctx = new HashMap<String, String>();
ctx.put("username", "Tarena");
System.out.println(Ognl.getValue("username", ctx, user));// user
System.out.println(Ognl.getValue("#username", ctx, user));// ‘#’ctx
} catch (OgnlException e) {
e.printStackTrace();
}
}
}