OGNL简单用法(单元测试方式的实例)

public class User {
private String userName;
private String password;

public User() {
super();
}

public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}

public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + "]";
}

}

-----------------------------------------------------------------------------------------------------

public class OGNLDemo1 {
@Test
public void demo1() throws OgnlException {
OgnlContext context = new OgnlContext();
Object root = context.getRoot();
Object obj = Ognl.getValue("'helloworld'.length()", context, root);
System.out.println(obj);
}

@Test
public void demo2() throws OgnlException {
OgnlContext context = new OgnlContext();
Object root = context.getRoot();
Object obj = Ognl.getValue("@java.lang.Math@random()", context, root);
System.out.println(obj);
}

@Test
public void demo3() throws OgnlException {
OgnlContext context = new OgnlContext();
User user = new User("aaa", "123");
context.setRoot(user);
Object root = context.getRoot();
Object userName = Ognl.getValue("userName", context, root);
Object password = Ognl.getValue("password", context, root);
System.out.println(userName + " " + password);
}

@Test
public void demo4() throws OgnlException {
OgnlContext context = new OgnlContext();
Object root = context.getRoot();
context.put("name", "张三");
Object obj = Ognl.getValue("#name", context , root);
System.out.println(obj);
}
}

posted @ 2019-01-16 21:00  李大鹏的博客  阅读(298)  评论(0编辑  收藏  举报