OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。
总的来说JSP页面中的元素<s:property value="xxx"/>,只要在action中有它对应的public Getter即可得到相应的值。
<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
<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
<s:iterator value="map" status="status">
<s:property value="key"/>
<s:property value="value"/>
</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
<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" />
分享到:
相关推荐
主要介绍了Struts2中ognl遍历数组,list和map方法详解,需要的朋友可以参考下。
struts2的ognl
本资源适合刚接触Struts和Ognl的小伙伴交流学习,欢迎吐槽。
主要介绍了Struts2 使用OGNL遍历map方法详解,具有一定参考价值,需要的朋友可以了解下。
从官网checkout下来的源码,如果大家觉的区官网麻烦可以直接下载这个
Struts2学习计划5---Struts2_OGNL(对象图导航语言)的学习
Struts2 OGNL---标签详解析 都有实例 适合初学者
struts2 ognl2.6.11源代码,官网上下要很麻烦,麻烦的事就我帮各位做了
关于struts2 OGNL 详细教程,对初学者有帮助
主要是ognl的用法,很适合新手上路的,源代码很清晰可见,一看就懂
对 Struts2 OGNL 的分析与简介
Struts2中ognl-2的源代码,从官网下载的
struts2 ognl源文件 在ECLIPSE导入后可方面的利于开发
struts2中ognl表达式的用法,详细介绍了ognl表达式与struts2基本标签的结合。
struts2OGNL表达式ActionContext及valuesStack.pdf
struts2 中 OGNL表达式的使用struts2 中 OGNL表达式的使用
NULL 博文链接:https://chaoyi.iteye.com/blog/2161928
Struts2的OGNL示例,使用Maven构建,导入请前先安装maven。
在struts2编程中,ognl帮助文档,你可导入到工程里,就可以查看相关文档。
博文参考:http://blog.csdn.net/u011638419/article/details/42041991