`

Struts2中OGNL的"#,%,$"使用!

阅读更多

OGNL是通常要结合Struts 2的标志一起使用,如<s:property value="xx" /> 等。大家经常遇到的问题是#、%和$这三个符号的使用。

 

 

“#”主要有三种用途:

  1. 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext();下表有几个ActionContext中有用的属性:
     名称 作用 例子
    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相当于按顺序在以上三个范围(scope)内读取userName属性,直到找到为止
  2. 用于过滤和投影(projecting)集合,如books.{?#this.price<100}
  3. 构造Map,如#{'foo1':'bar1', 'foo2':'bar2'}

 

“%”符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。例如在Ognl.jsp中加入以下代码:

< hr />
   
< h3 > %的用途 </ h3 >
   
< p >< s:url value ="#foobar['foo1']" /></ p >
   
< p >< s:url value ="%{#foobar['foo1']}" /></ p >


 

“$”有两个主要的用途

  1. 用于在国际化资源文件中,引用OGNL表达式,例子请参考《在Struts 2.0中国际化(i18n)您的应用程序
  2. 在Struts 2配置文件中,引用OGNL表达式,如
    < action name ="AddPhoto" class ="addPhoto" >
               
    < interceptor-ref name ="fileUploadStack" />            
               
    < result type ="redirect" > ListPhotos.action?albumId=${albumId} </ result >
           
    </ action >

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics