ogn表达式

来源:互联网 发布:忽略此网络如何恢复 编辑:程序博客网 时间:2024/05/16 13:51

        两者都是在服务器端执行的,JSP在转化成Servelt并编译成java文件时,会将ognl,el,<%%>等解释出来,然后返回给客户端;

    ognl表达式需要依赖于Struts2的标签,如<s:property  value="#xxxx">   在Struts的页面中不能使用,如使用<a href="checkInfo.jsp?id=%{#student.studentid}">但是可以使用<s:a href="checkInfo.jsp?id=%{#student.studentid}">因为<s:a>是Struts2的标签

说明:<s:a href="checkInfo.jsp?id=%{#student.studentid}">中的%{}的作用是告诉解释器其中的代码为ognl表达式;

但是el表达式是可以的如${sessionScope.username}

页面区中的区别:

 名称

                                                          servlet                                                                 ognl                                                                           el

parameters                                   request.getParameter("username")             #username                                                  ${username}

request                                          request.getAttribute("userName")                 #request.userName                                ${requestScope.username}

session                                          session.getAttribute("userName")                 #session.userName                               ${sessionScope.username}

application                                   application.getAttribute("userName")            #application.userName                          ${applicationScope.username}

attr       用于按request > session > application顺序访问其属性(attribute      #attr.userName相当于按顺序在以上三个范围(scope)内读取userName属性,直到找到为


OGNL的讲解:    

ognl是Struts2默认的语言,也成为(对象图导航语言)

1.#号的用途一般有三种:
a.访问非根对象的属性:例如#session.username,在Struts2中值栈被认为是根对象,所以在访问非根对象时前面要添加上#作为前缀,实际上#号相当于ActionContext.getContext(),所以#session.username相当于ActionContext.getContext().getSession().getAttribute("username");

b.用于过滤和投影(projecting)集合,如:persons.{?#this.age>20}

c.用于构建map,如#{‘foot1’:'bar1','foot2':'bar2'}

2.%的用途:

用途是在标志的属性为字符串属性时计算OGNL表达式的值,

  1. <s:set name=”foobar” value=”#{’foo1′:’bar1′, ‘foo2′:’bar2′}” />  
  2. <p>The value of key “foo1″ is <s:property value=”#foobar['foo1']” /></p>  
  3. <p>不使用%:<s:url value=”#foobar['foo1']” /></p>  
  4. <p>使用%:<s:url value=”%{#foobar['foo1']}” /></p>   
3.$的用途:

$号的用途主要有两个:在国际化资源文件中引用OGNL表达式,例如国际化中的代码reg.agerange=国际化资源信息:年龄介于${min}和${max}年龄之间

在Struts2中引用OGNL表达式,如下面的代码:

  1. <validators>  
  2.     <field name=”intb”>  
  3.             <field-validator type=”int”>  
  4.             <param name=”min”>10</param>  
  5.             <param name=”max”>100</param>  
  6.             <message>BAction-test校验:数字必须为${min}为${max}之间!</message>  
  7.         </field-validator>  
  8.     </field>  
  9. </validators>  

0 0
原创粉丝点击