OGNL

来源:互联网 发布:qe 知乎 编辑:程序博客网 时间:2024/05/20 04:11

OBject-Graph Navigation Language

package ognlpackage;import java.util.ArrayList;import java.util.List;import ognl.Ognl;import ognl.OgnlContext;import ognl.OgnlException;public class OGNLBasic {private static Group group = new Group();private static List<User>users = new ArrayList<User>();private static User user1 = new User();private static User user2 = new User();private static OgnlContext ognlContext = new OgnlContext();private static Object root = new Object();//根对象,只做点位符使用public static final String HELLO_WORLD="hello world";private static void initData(){group.setGroupId(1000L);group.setGroupName("OGNL组");user1.setUserId(1L);user1.setUserName("zs");user1.setGroup(group);user2.setUserId(2L);user2.setUserName("ls");user2.setGroup(group);users.add(user1);users.add(user2);group.setUsers(users);//双向关联ognlContext.put("group", group);//全部放入到OGNLContext中,其实就是一个mapognlContext.put("users", users);ognlContext.put("user1", user1);ognlContext.put("user2", user2);}private static void printGroup() throws OgnlException{System.out.println("      1.  以group为OGNL对象访问用户组信息       ");System.out.println("用户组的ID为:"+Ognl.getValue("groupId", group));System.out.println("用户组的名称为:"+Ognl.getValue("groupName", group));System.out.println("-----------------------------------------------");}private static void printUsers() throws OgnlException{System.out.println("     2. 以group为OGNL root对象访问用户组中用户的信息     ");System.out.println("用户组1为:"+"id="+ Ognl.getValue("users[0].userId", group));System.out.println("用户组1为:"+"name="+ Ognl.getValue("users[0].userName", group));System.out.println("-----------------------------------------------");}/** * 切换OGNLContext访问对象 * @throws OgnlException */private static void switchOGNLContext() throws OgnlException{System.out.println("     3. 切换OGNLContext访问对象 需要加  #     ");System.out.println("用户组id:" + Ognl.getValue("groupId + ',用户1的ID为 '+ #user1.userId", ognlContext, group));//可以看出,无论对象切换到哪个,都可以用    #对象.属性表示System.out.println("用户组id:" + Ognl.getValue("#group.groupId + ',用户2的ID为 '+ #user2.userId", ognlContext, user2));System.out.println("用户组id:" + Ognl.getValue("#group.groupId + ',用户2的ID为 '+ userId", ognlContext, user2));System.out.println("-----------------------------------------------");}/** * 访问静态变量要在前面加  @ * @throws OgnlException */private static void printStatic() throws OgnlException{System.out.println("     4. 访问静态变量要在前面加  @     ");Ognl.getValue("@System@out.println(@ognlpackage.OGNLBasic@HELLO_WORLD)", root);System.out.println("-----------------------------------------------");}private static void printThis() throws OgnlException{System.out.println("     5. 支持赋值,this的使用    ");//OGNL导航至group.users.size方法时,使用#this来判断size的返回值是否大于0System.out.println("用户组是否为空:" + Ognl.getValue("users.size.(#this>0?'否':'是')", group));System.out.println("-----------------------------------------------");}public static void main(String[] args) throws OgnlException{initData();printGroup();printUsers();switchOGNLContext();printStatic();printThis();}}class Group{private Long groupId;private String groupName;private List<User>users;public List<User> getUsers() {return users;}public void setUsers(List<User> users) {this.users = users;}public Long getGroupId() {return groupId;}public void setGroupId(Long groupId) {this.groupId = groupId;}public String getGroupName() {return groupName;}public void setGroupName(String groupName) {this.groupName = groupName;}}class User{private Long userId;private String userName;private Group group;public Group getGroup() {return group;}public void setGroup(Group group) {this.group = group;}public Long getUserId() {return userId;}public void setUserId(Long userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}}

输出结果:

      1.  以group为OGNL对象访问用户组信息       用户组的ID为:1000用户组的名称为:OGNL组-----------------------------------------------     2. 以group为OGNL root对象访问用户组中用户的信息     用户组1为:id=1用户组1为:name=zs-----------------------------------------------     3. 切换OGNLContext访问对象 需要加  #     用户组id:1000,用户1的ID为 1用户组id:1000,用户2的ID为 2用户组id:1000,用户2的ID为 2-----------------------------------------------     4. 访问静态变量要在前面加  @     hello world-----------------------------------------------     5. 支持赋值,this的使用    用户组是否为空:否-----------------------------------------------



0 0
原创粉丝点击