`
yangzhibin_java
  • 浏览: 50938 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

ognl

阅读更多

一,应用场景

  1,标签上:如: <s:property value="user.name ">

           value属性中自动支持 ognl表达式

  2,配置文件中: 如: <result type="redirect">/main.jsp?name=${name} </result>

  3,表单中: 如: <input type="text" name="user.name "/>

 

二,获取值栈

1,普通属性/方法
对象的属性:<s:property value="user.username "/>
对象的普通方法:<s:property value="user.get() "/>

 

2.取Action方法

          调用action中的静态方法:<s:property value="@vs1 @get() "/> --如果值栈中只有一个action就是用vs
          调用action中的静态方法:<s:property value="@vs2 @get() "/>


3,集合

     2.1List
        获取List:<s:property value=" testList "/>
        获取List中的某一个元素:<s:property value=" testList[0] "/>

        //利用伪属性
        获取List的大小:<s:property value=" testSet . size "/>

        // 利用投影
        获取List中的对象的username属性:<s:property value=" studentList.{username} "/>
        获取List中的 第一个对象 的username属性:<s:property value=" studentList.{username}[0] "/>利用投影

        // 利用投影 +选择   

        选择基础知识:

         ?:表示取出所有对象

         ^:表示开始,取第一个元素

        获取List中成绩及格的对象:<s:property value=" studentList .{?#this.grade>=60} "/>
        获取List中成绩及格的对象的username:<s:property value=" studentList .{?#this.grade>=60}.{username} "/>
        获取List中成绩及格的第一个对象的username:<s:property value=" studentList .{?#this.grade>=60}.{username}[0] "/>   ---返回String(数组越界也不报异常)
        获取List中成绩及格的第一个对象的username:<s:property value=" studentList .{^#this.grade>=60}.{username} "/>     ---返回一个list

获取List中成绩及格的第一个对象的username:<s:property value=" studentList .{^#this.grade>=60}.{username}.size "/>     ---用来判断是否有元素
        获取List中成绩及格的最后一个对象的username:<s:property value=" studentList .{$#this.grade>=60}.{username} "/>
        获取List中成绩及格的第一个对象然后求大小:<s:property value=" studentList .{^#this.grade>=600}.{username}.size "/>
    2.2Set
        获取Set:<s:property value=" testSet "/>
        获取Set中的某一个元素:<s:property value=" testSet[0] "/> ------- 错误
    2.3Map
        获取Map:<s:property value=" testMap "/>
        获取Map中的某一个元素:<s:property value=" testMap['m1'] "/>
       
        //利用伪属性
        获取Map中所有的键:<s:property value=" testMap.keys "/>
        获取Map中所有的值:<s:property value=" testMap.values "/>


三,获取非值栈(@包+类@方法or 属性)

  注: 普通类的普通方法不给调( 只能调用普通类的静态方法静态属性)
1,JDK
    JDK中的类的静态方法:<s:property value="@java.lang.Math@floor(44.56) "/>
    JDK中的类的静态方法:<s:property value="@@floor(44.56) "/>  (同上)
    JDK中的类的静态方法:<s:property value="@java.util.Calendar@getInstance() "/>

2.普通类
    普通类中的静态属性:<s:property value="@cn.yangzhibin.Constant@AAA "/>
    普通类的构造方法:<s:property value="new cn.yangzhibin.Student('张三','北京大学',30).name "/>

四,获取request,session里的属性
    获取request范围中的属性:<s:property value="#request.req "/>
    获取session范围中的属性:<s:property value="#session.ses "/>
    获取attr中的属性:<s:property value="#attr.aaa "/>    

五,Lambda表达式
    使用Lambda表达式计算阶乘:<s:property value="#f = :[#this==1?1:#this*#f(#this-1)] , #f(4)"/>  

 

六,ognl中符号的使用

    6.1,# 的用法( 可以取出堆栈上下文中的存放的对象

             # :不仅可以读取堆栈上下文中的存放的对象,还可以读取在定义<s:bean>定义定义的对象

名称
作用
例子
parameters
包含当前 HTTP 请求参数的 Map
#parameters.id[0] 作用相当于 request.getParameter("id")
request
包含当前 HttpServletRequest 的属性 attribute) Map
#request.userName 相当于 request.getAttribute("userName")
session
包含当前 HttpSession 的属性 attribute )的 Map
#session.userName 相当于 session.getAttribute("userName")
application
包含当前应用的 ServletContext 的属 性( attribute )的 Map
#application.userName 相当于 application.getAttribute("userName")
attr
用于按 request > session > application 顺序访问其属性 attribute
#attr.userName 相当于 从上面所有的对象范围内取一遍,取范围最小的那个对象

 

     6.2,% 的用法

         %{},----直接找action

         可以取出存在值堆栈中的Action对象 ,直接调用它的方法.

         如:用%{getText('key')} 的方式可以拿出国际化信息

 

     6.3 $ 的用法

         用途一: 用于在国际化资源文件中,引用OGNL表达式
         用途二: 在Struts 2配置文件中,引用OGNL表达式

 

七, 值栈(ValueStack对象)可以省写#

       栈:先进后出

       查看值栈中的信息:<s:debug></s:debug>

       7.1生命周期

           值栈的生命周期 == 一个request请求(每一次请求产生一个值

 

 

 

 

 

分享到:
评论

相关推荐

    OGNL百度百科OGNL百度百科

    OGNL百度百科OGNL百度百科OGNL百度百科OGNL百度百科OGNL百度百科OGNL百度百科

    ognl表达式java使用案例详解(测试通过)

    本案例提供ognl使用详解,测试通过,只需解压放入自己WEB项目中,执行struts_ognl包内java文件即可(未提供jia包,若需要可以联系留言发至邮箱),若测试不好可以联系本人提供指导. Struts 2默认的表达式语言是OGNL...

    开发工具 ognl-3.1.12

    开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12开发工具 ognl-3.1.12...

    OGNL教程,简单,看完懂OGNL语言

    OGNL 最初是为了能够使用对象的属性名来建立 UI 组件 (component) 和 控制器 (controllers) 之间的联系,简单来说就是:视图 与 控制器 之间数据的联系。后来为了应付更加复杂的数据关系,Drew Davidson 发明了一个...

    ognl.jar资源包

    camel-ognl-1.6.4.jar, camel-ognl-2.8.1.jar, com.springsource.org.ognl-2.6.9.jar, com.springsource.org.ognl-sources-2.6.9.jar, ognl-2.5.1.jar, ognl-2.6.11.jar, ognl-2.6.3.jar, ognl-2.6.5.jar, ognl-...

    ognl-3.1.12-API文档-中文版.zip

    赠送jar包:ognl-3.1.12.jar; 赠送原API文档:ognl-3.1.12-javadoc.jar; 赠送源代码:ognl-3.1.12-sources.jar; 赠送Maven依赖信息文件:ognl-3.1.12.pom; 包含翻译后的API文档:ognl-3.1.12-javadoc-API文档-...

    ognl-3.2.21-API文档-中文版.zip

    赠送jar包:ognl-3.2.21.jar; 赠送原API文档:ognl-3.2.21-javadoc.jar; 赠送源代码:ognl-3.2.21-sources.jar; 赠送Maven依赖信息文件:ognl-3.2.21.pom; 包含翻译后的API文档:ognl-3.2.21-javadoc-API文档-...

    ognl-3.2.21-API文档-中英对照版.zip

    赠送jar包:ognl-3.2.21.jar; 赠送原API文档:ognl-3.2.21-javadoc.jar; 赠送源代码:ognl-3.2.21-sources.jar; 赠送Maven依赖信息文件:ognl-3.2.21.pom; 包含翻译后的API文档:ognl-3.2.21-javadoc-API文档-...

    ognl表达式 ognl表达式

    ognl表达式 ognl表达式 ognl表达式 ognl表达式

    OGNL中文版详细文档

    OGNL特有的对象索引属性(Object Indexed Properties) 调用方法 变量引用 带括号的表达式 子表达式(Subexpressions)链 构造集合(Collection) 列表(Lists) 数组 映射表(Maps) 对集合的投影(Project) 在集合...

    ognl-3.1.12-API文档-中英对照版.zip

    赠送jar包:ognl-3.1.12.jar; 赠送原API文档:ognl-3.1.12-javadoc.jar; 赠送源代码:ognl-3.1.12-sources.jar; 赠送Maven依赖信息文件:ognl-3.1.12.pom; 包含翻译后的API文档:ognl-3.1.12-javadoc-API文档-...

    ognl-2.6.11.jar 官方源码

    ognl-2.6.11.jar 源码 ognl-2.6.11.jar 源码

    struts2.0中的ognl

    基于WebWork核心的MVC开发与实践(ognl这一章)

    OGNL表达式的使用及文档

    关于OGNL表达式的使用的整理以及提供的文档说明,ps:文档说明是英文的,可能需要翻译

    ognl-3.0.5-sources.jar

    ognl-3.0.5源码 本人最近在研究s2sh源码,如有需要请留言。 在使用过程中如果有问题可联系,让我们开始学习s2sh源码总动员吧!

    Ognl在Struts2中的应用 源码

    本资源适合刚接触Struts和Ognl的小伙伴交流学习,欢迎吐槽。

    使用的ognl的chm

    使用的ognl,正对是struts2 hibernate 等

    ognl源码.rar

    ognl源码 ognl源码 ognl源码 ognl源码 ognl源码

    ognl.jar ognl.jar

    ognl.jar ognl.jar

    ognl-2.6.11.jar 的使用源码。EL语言的使用实例

    OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,...

Global site tag (gtag.js) - Google Analytics