`
sd8089730
  • 浏览: 251487 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
社区版块
存档分类
最新评论

Struts2 Ognl遍历(转)

阅读更多

OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。 
总的来说JSP页面中的元素<s:property value="xxx"/>,只要在action中有它对应的public Getter即可得到相应的值。 



Java代码 复制代码 收藏代码
  1. <s:select label="label" name="name" list="{'name1','name2','name3'}" value="%{'name2'}" />    
  2.   
  3. <s:select label="userList" name="userList" list="userList" listKey="id" listValue="name" /> // userList中为JavaBean    
  4.   
  5. <s:select label="'map'" name="mapList" list="map"/>   
<s:select label="label" name="name" list="{'name1','name2','name3'}" value="%{'name2'}" /> 

<s:select label="userList" name="userList" list="userList" listKey="id" listValue="name" /> // userList中为JavaBean 

<s:select label="'map'" name="mapList" list="map"/> 

 
选择(select)一个集合的子集(叫做投影),你可以在Collection中使用通配符. 

 

? -所有符合选择逻辑的元素 
^ - 符合选择逻辑的第一个元素
 
$ - 符合选择逻辑的最后一个元素 
示例: 
JavaBean 
Book.java 
private String title; 
private String author; 
private double price; 

Action: private List<Book> bookList; 
JSP页面: 

<s:iterator value="bookList.{?#this.price > 35}"> 
      <s:property value="title" /> :<s:property value="price" /> 
</s:iterator> 


Iterator 


遍历List 

 

 

Java代码 复制代码 收藏代码
  1. <s:iterator value="userList" status="status">   
  2. //userList来自Action的public List getUserList()得到,无参的Getter,且修饰符一定为public   
  3.       <s:set name="user"/>   
  4.       <s:property value="name"/>   
  5.       <s:property value="#user.name"/>//来自JSP页面的变量得用#引用   
  6.       <s:property value="top.name"/>   
  7.       <s:property value="top"/>   
  8.       <s:property value="getValue(#user)"/>//getValue为Action中定义的public User getValue(User user)    
  9. </s:iterator>  
<s:iterator value="userList" status="status">
//userList来自Action的public List getUserList()得到,无参的Getter,且修饰符一定为public
      <s:set name="user"/>
      <s:property value="name"/>
      <s:property value="#user.name"/>//来自JSP页面的变量得用#引用
      <s:property value="top.name"/>
      <s:property value="top"/>
      <s:property value="getValue(#user)"/>//getValue为Action中定义的public User getValue(User user) 
</s:iterator>

 

 

遍历Map 

 

 

Java代码 复制代码 收藏代码
  1. <s:iterator value="map" status="status">   
  2.      <s:property value="key"/>   
  3.      <s:property value="value"/>   
  4. </s:iterator>  
<s:iterator value="map" status="status">
     <s:property value="key"/>
     <s:property value="value"/>
</s:iterator>

 
例子

 

 

 

Java代码 复制代码 收藏代码
  1. <s:iterator value="{'a','b','c'}" status="status" >   
  2.       <s:property value="#status.index" />   
  3.       <s:property value="#status.count-1" />   
  4.       <s:property value="top" />-   
  5.       <s:property value="'top'+#status.index+':'+top" />   
  6. </s:iterator>  
<s:iterator value="{'a','b','c'}" status="status" >
      <s:property value="#status.index" />
      <s:property value="#status.count-1" />
      <s:property value="top" />-
      <s:property value="'top'+#status.index+':'+top" />
</s:iterator>

 

 

输出 
0 0 a- top0:a 
1 1 b- top1:b 
2 2 c- top2:c 


iterator 中status 的方法: 

even : boolean - returns true if the current iteration is even 
odd
  : boolean - returns true if the current iteration is odd 
count
 : int - returns the count (1 based) of the current iteration 
index
 : int - returns the index (0 based) of the current iteration 
first
 : boolean - returns true if the iterator is on the first iteration 
last
 : boolean - returns true if the iteration is on the last iteration 
modulus
(operand : int) : int - returns the current count (1 based) modulo the given operand 
eg:<s:property value="#stat.modulus(2)" /> 


IF 

 

 

Java代码 复制代码 收藏代码
  1. <s:if test="#status.odd">class1</s:if><s:else>class2</s:else>   
  2. <s:if test="type=='Amount'||type==null"> display</s:if>   
  3. //其中type相当<ww:property value="type" />   
<s:if test="#status.odd">class1</s:if><s:else>class2</s:else>
<s:if test="type=='Amount'||type==null"> display</s:if>
//其中type相当<ww:property value="type" /> 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics