---
title: "ognl"
author: "yangzhibin_java"
date: "2009-08-05T00:12:43Z"
tags: []
slug: "ognl"
draft: false
lastmod: "2009-08-06T11:53:51Z"
id: 440314
url: "https://yangzhibin-java.iteye.com/blog/440314"
---

**一，应用场景**

  1，**标签上：如：** &lt;s:property value="**user.name** "&gt;

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

  2，**配置文件中：** **如：** &lt;result type="redirect"&gt;/main.jsp?name=**${name}** &lt;/result&gt;

  3，**表单中：** **如：** &lt;input type="text" name="**user.name** "/&gt;

 

**二，获取值栈**

**1，普通属性/方法**  
对象的属性:&lt;s:property value="**user.username** "/&gt;  
对象的普通方法：&lt;s:property value="**user.get()** "/&gt;

 

**2.取Action方法**

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

**3，集合**  
     **2.1List**  
        获取List:&lt;s:property value=" **testList** "/&gt;  
        获取List中的某一个元素:&lt;s:property value=" **testList\[0]** "/&gt;

        **//利用伪属性**  
        获取List的大小:&lt;s:property value=" **testSet** . **size** "/&gt;

        // **利用投影**  
        获取List中的对象的username属性：&lt;s:property value=" **studentList.{username}** "/&gt;  
        获取List中的 **第一个对象** 的username属性：&lt;s:property value=" **studentList.{username}\[0]** "/&gt;利用投影

        // **利用投影** +**选择**   

        选择基础知识：

         ？：表示取出所有对象

         ^：表示开始，取第一个元素

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

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

**三，获取非值栈（@包+类@方法or 属性）**

  **注： 普通类的普通方法不给调（** **只能调用普通类的静态方法 和静态属性）**  
**1，JDK**  
    JDK中的类的静态方法：&lt;s:property value="**@java.lang.Math@floor(44.56)** "/&gt;  
    JDK中的类的静态方法：&lt;s:property value="**@@floor(44.56)** "/&gt;  (同上)  
    JDK中的类的静态方法：&lt;s:property value="**@java.util.Calendar@getInstance()** "/&gt;

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

**四，获取request，session里的属性**  
    获取request范围中的属性：&lt;s:property value="**#request.req** "/&gt;  
    获取session范围中的属性：&lt;s:property value="**#session.ses** "/&gt;  
    获取attr中的属性：&lt;s:property value="**#attr.aaa** "/&gt;    

**五，Lambda表达式**  
    使用Lambda表达式计算阶乘:&lt;s:property value="#f = :\[#this==1?1:#this\*#f(#this-1)] , #f(4)"/&gt;  

 

**六，ognl中符号的使用**

    **6.1，# 的用法（** **可以取出堆栈上下文中的存放的对象 ）**

             **#** ：不仅可以读取**堆栈上下文中的存放的对象，还可以读取在定义&lt;s:bean&gt;定义定义的对象**

**名称**

**作用**

**例子**

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 &gt; session &gt; application 顺序访问其属性 （ attribute ）

#attr.userName 相当于 从上面所有的对象范围内取一遍，取范围最小的那个对象

 

     **6.2，% 的用法**

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

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

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

 

     **6.3** **，$ 的用法**

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

 

**七，** **值栈（ValueStack对象）可以省写#**

       **栈：先进后出**

       查看值栈中的信息：&lt;s:debug&gt;&lt;/s:debug&gt;

       **7.1生命周期**

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