首先了解下OGNL的概念:        OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调用对象的方法,能够遍历整个对象的结构图,实现对象属性类型的转换等功能。        1.OGNL表达式的计算是围绕OGNL上下文进行的。    OGNL上下文实际上就是一个Map对象,由ognl.OgnlContext类表示。    上下文中的根对象ValueStack可以直接使用名来访问或直接使用它的属性名访问它的属性值。Struts2总是把当前Action实例放置在栈顶。所以在OGNL中引用Action中的属性也可以省略“#”。否则要加前缀“#key”。2.Struts2的标签库都是使用OGNL表达式来访问ActionContext中的对象数据的。如:                
。          OGNL中重要的3个符号:#、%、$:1.#符号    #符号的用途一般有三种。        (1)访问非根对象属性,例如#session.msg表达式,由于Struts 2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀。实际上,#相当于ActionContext. getContext();#session.msg表达式相当于ActionContext.getContext().getSession(). getAttribute("msg") 。       (2)用于过滤和投影(projecting)集合,如persons.{?#this.age>25},persons.{?#this.name=='pla1'}.{age}[0]。        (3)用来构造Map,例如示例中的#{'foo1':'bar1', 'foo2':'bar2'}。2.%符号    %符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值,这个类似js中的eval,很暴力。    3.$符号    $符号主要有两个方面的用途。    (1)在国际化资源文件中,引用OGNL表达式,例如国际化资源文件中的代码:reg.agerange=国际化资源信息:年龄必须在${min}同${max}之间。    (2)在Struts 2框架的配置文件中引用OGNL表达式,例如:
        
                
                
10                
100                
BAction-test校验:数字必须为${min}为${max}之间!
                        
下面通过代码对OGNL有更深的了解:action类OgnlAction.java:package com.tjcyjd.test.action;    import java.util.Date;  import java.util.LinkedList;  import java.util.List;    import javax.servlet.http.HttpServletRequest;    import org.apache.struts2.ServletActionContext;  import org.apache.struts2.convention.annotation.Action;  import org.apache.struts2.convention.annotation.Namespace;  import org.apache.struts2.convention.annotation.ParentPackage;  import org.apache.struts2.convention.annotation.Result;  import org.apache.struts2.convention.annotation.Results;  import org.springframework.stereotype.Controller;    import com.opensymphony.xwork2.ActionContext;  import com.opensymphony.xwork2.ActionSupport;    @Controller  @Namespace("/test")  @ParentPackage("struts-default")  @Results( { @Result(name = "success", location = "/other_test/showognl.jsp"),          @Result(name = "fail", location = "/bbs/admin_login.jsp"),          @Result(name = "input", location = "/bbs/admin_login.jsp") })  public class OgnlAction extends ActionSupport {      private static final long serialVersionUID = -1494290883433357310L;      private List
 persons;        @Action("ognlTest")      public String ognlTest() throws Exception {          // 获得ActionContext实例,以便访问Servlet API          ActionContext ctx = ActionContext.getContext();          // 存入application          ctx.getApplication().put("msg", "application信息");          // 保存session          ctx.getSession().put("msg", "seesion信息");          // 保存request信息          HttpServletRequest request = ServletActionContext.getRequest();          request.setAttribute("msg", "request信息");          // 为persons赋值          persons = new LinkedList
();          Person person1 = new Person();          person1.setName("pla1");          person1.setAge(26);          person1.setBirthday(new Date());          persons.add(person1);            Person person2 = new Person();          person2.setName("pla2");          person2.setAge(36);          person2.setBirthday(new Date());          persons.add(person2);            Person person3 = new Person();          person3.setName("pla3");          person3.setAge(16);          person3.setBirthday(new Date());          persons.add(person3);            return SUCCESS;        }        public List
 getPersons() {          return persons;      }        public void setPersons(List
 persons) {          this.persons = persons;      }  }jsp页面showognl.jsp:<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>        <%@ taglib prefix="s" uri="/struts-tags" %>            
                    
Struts2 OGNL 演示                                

访问OGNL上下文和Action上下文

            
            

parameters: 

            

request.msg: 

            

session.msg: 

            

application.msg: 

            

attr.msg: 

            
            

用于过滤和投影(projecting)集合

            

年龄大于20

            
                
                    
    20}">                    
  •  - 年龄:
  •                             
            

姓名为pla1的年龄: 

            
            

构造Map

            
            

The value of key "foo1" is 

              
             

%符号的用法

            
            

The value of key "foo1" is 

            

不使用%:

            

使用%:

            
          <%                request.setAttribute("req", "request scope");                request.getSession().setAttribute("sess", "session scope");                request.getSession().getServletContext().setAttribute("app",                        "aplication scope");            %>            1.通过ognl表达式获取 属性范围中的值            
            
            
            
            
            
            
            

        2.通过
ognl表达式创建list 集合 ,并且遍历出集合中的值            
            
            
                
                
                
                        
            

        3.通过ognl表达式创建Map 集合 ,并且遍历出集合中的值            
            
            
                
                
                
->
                
                        
            

           4.通过ognl表达式 进行逻辑判断            
            
                aa 在 集合{'aaa','bbb'}中;                        
                aa 不在 集合{'aaa','bbb'}中;            
            
            
                    不 在 集合list中;                        
                 在 集合list中;            
            
            

              5.通过ognl表达式 的投影功能进行数据筛选            
            
            
2}" var="o">                
                ${o }
                        
            

    6.通过ognl表达式 访问某个类的静态方法和值            
            
                
            
            
            

          7.ognl表达式 迭代标签 详细            
            
            
                                    索引                     值                    奇?                     偶?                    首?                     尾?                    当前迭代数量                                
                    
pink">                                                    
                                                                            
                                                                            
Y                            
N
                                                                            
Y                            
N
                                                                            
Y                            
N
                                                                            
Y                            
N
                                                                        
                                                                                
            

                   8.ognl表达式:  if/else if/else 详细
            <% request.setAttribute("aa",0); %>            
=0 && #request.aa<=4">                    在0-4之间;                        
=4 && #request.aa<=8">                    在4-8之间;                        
                 大于8;            
            
            

      9.ognl表达式: url 详细
            <% request.setAttribute("aa","sss"); %>            
                
                
100                        
            
            value以字符处理:   
            value明确指定以ognl表达式处理:    
            
            

    10.ognl表达式: checkboxlist 详细
            1> .list 生成;
            name:checkboxlist的名字
            list:checkboxlist要显示的列表
            value:checkboxlist默认被选中的选项,checked=checked
            
            
            以上生成代码:
            
<div></div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox1"&nbsp;value="上网"&nbsp;id="checkbox1-1"&nbsp;checked="checked"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox1-1"&nbsp;class="checkboxLabel"&gt;上网&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox1"&nbsp;value="看书"&nbsp;id="checkbox1-2"&nbsp;checked="checked"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox1-2"&nbsp;class="checkboxLabel"&gt;看书&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox1"&nbsp;value="爬山"&nbsp;id="checkbox1-3"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox1-3"&nbsp;class="checkboxLabel"&gt;爬山&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox1"&nbsp;value="游泳"&nbsp;id="checkbox1-4"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox1-4"&nbsp;class="checkboxLabel"&gt;游泳&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox1"&nbsp;value="唱歌"&nbsp;id="checkbox1-5"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox1-5"&nbsp;class="checkboxLabel"&gt;唱歌&lt;/label&gt;"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div></div>                     2> .Map 生成;
            name:checkboxlist的名字
            list:checkboxlist要显示的列表
            listKey:checkbox 的value的值
            listValue:checkbox 的lablel(显示的值)
            value:checkboxlist默认被选中的选项,checked=checked
            
            
        以上生成代码:
            
<div></div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox2"&nbsp;value="1"&nbsp;id="checkbox2-1"&nbsp;checked="checked"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox2-1"&nbsp;class="checkboxLabel"&gt;上网&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox2"&nbsp;value="2"&nbsp;id="checkbox2-2"&nbsp;checked="checked"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox2-2"&nbsp;class="checkboxLabel"&gt;看书&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox2"&nbsp;value="3"&nbsp;id="checkbox2-3"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox2-3"&nbsp;class="checkboxLabel"&gt;爬山&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox2"&nbsp;value="4"&nbsp;id="checkbox2-4"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox2-4"&nbsp;class="checkboxLabel"&gt;游泳&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="checkbox"&nbsp;name="checkbox2"&nbsp;value="5"&nbsp;id="checkbox2-5"&nbsp;checked="checked"/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label&nbsp;for="checkbox2-5"&nbsp;class="checkboxLabel"&gt;唱歌&lt;/label&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div></div>             

        OGNL中%的使用    用%{}可以取出存在值堆栈中的Action对象,直接调用它的方法.例如你的Action如果继承了ActionSupport .那么在页面标签中,用%{getText('key')}的方式可以拿出国际化信息.OGNL中$的使用 “$”有两个主要的用途        用于在国际化资源文件中,引用OGNL表达式       在Struts 2配置文件中,引用OGNL表达式       值栈    ValueStack对象。这个对象贯穿整个Action的生命周期(每个Action类的对象实例会拥有一个ValueStack对象)。当Struts 2接收到一个.action的请求后,会先建立Action类的对象实例,但并不会调用Action方法,而是先将Action类的相应属性放到ValueStack对象的顶层节点(ValueStack对象相当于一个栈)。    在Action中获得ValueStack对象:ActionContext.getContext().getValueStack()Top语法    使用Top获取值栈中的第二个对象:
N语法    使用N获取值栈中的第二个对象:
@语法    调用action中的静态方法:
 vs:值栈 1:表示第一个。