struts2—OGNL

1.OGNL表达式

  1.ONGL是对象视图导航语言 ${user.name}这种写法就是对象视图导航

  2.OGNL的jar包是包含在Struts2基础包中

  3.OGNL的存储方式

    

  4.OGNL表达式使用方式

public void ognlBasc() throws OgnlException {
        OgnlContext oc = new OgnlContext();
        User user = new User("zjj","123456");
        oc.setRoot(user);
        Map<String , User> map = new HashMap<>();
        map.put("user1", new User("xjh","123456"));
        map.put("user2", new User("tjl","3216465"));
        oc.setValues(map);
        //获得root里的内容
        String result1 = (String) Ognl.getValue("name", oc, oc.getRoot());
        //获得map里的内容 用#标识是获得是map集合的内容咯不能
        String result2 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
        //调用方法获取字符串长度
        Integer result3 = (Integer) Ognl.getValue("#user1.name.length()", oc, oc.getRoot());
        //为属性赋值
        String result4 = (String) Ognl.getValue("#user1.name='xjh-zjj',#user1.name", oc, oc.getRoot());
        //调用静态方法/@全类名@方法名
         Double result5 = (Double) Ognl.getValue("@@pow(2,3)", oc, oc.getRoot()); 
         Double result6 = (Double) Ognl.getValue("@@PI", oc, oc.getRoot()); 
         //ognl创建对象-list|map
         //1.创建list对象
          String result7 =(String) Ognl.getValue("{'zjj','xjh'}[0]", oc, oc.getRoot());
         //2.创建map集合
          String result8 =(String)Ognl.getValue("#{'user':'123','age':'123'},#user",oc,oc.getRoot());
        System.out.println(result7);
    }

2.OGNL与Struts2的结合

OGNL中的OGNLContext------------>valueStack值栈

  值栈(valueStack)由两部分构成

  一部分叫做Root,放置的是一个栈

  一个部分叫做Context

栈是先进后出的

3.OGNL标签

<!-- 取map的两种方式 -->
<s:property value="#map['user1'].password"/><br/>
<s:property value="#map.user1.name"/><br>
<hr>
<!-- 循环过滤功能 -->
<s:iterator value="#map.{?#this.name.length()==3}">
    <s:property value="name"/><br/>
</s:iterator>
<hr>

猜你喜欢

转载自www.cnblogs.com/FlyBlueSky/p/9164702.html