struts2:OGNL表达式,遍历List、Map集合;投影的使用
2013-12-10 15:38
661 查看
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。
request attribute para = request scope attribute
session attribute para = session scope attribute
application attribute para = aplication scope attribute
attr attribute para = request scope attribute
输出:
eeeee
ddddd
ccccc
bbbbb
aaaaa
输出:
1->eeeee
2->ddddd
3->ccccc
4->bbbbb
5->aaaaa
输出:
aaa 在 集合{'aaa','bbb'}中;
不 在 集合list中;
输出:
张三 赵五 钱七
张三
钱七
张三
赵五
输出:
32.0
输出:
View Code
struts2:遍历自定义字符串数组,遍历Action实例所引用对象中的数组
片头(ognl.jsp)
<% request.setAttribute("para", "request scope attribute"); request.getSession().setAttribute("para", "session scope attribute"); request.getSession().getServletContext().setAttribute("para", "aplication scope attribute"); List<Student> listStudent = new ArrayList<Student>(); listStudent.add(new Student(1, "张三", new Date())); listStudent.add(new Student(2, "李四", new Date())); listStudent.add(new Student(3, "赵五", new Date())); listStudent.add(new Student(4, "孙六", new Date())); listStudent.add(new Student(5, "钱七", new Date())); pageContext.setAttribute("listStudent", listStudent); %>
调用
http://127.0.0.1:8080/st/ssh/demo6/ognl.jsp?para=hello1. 通过OGNL表达式获取属性范围中的值
<br/> HTTP parameters para = <s:property value="#parameters.para" /> <br/> request attribute para = <s:property value="#request.para" /> <br/> session attribute para = <s:property value="#session.para" /> <br/> application attribute para = <s:property value="#application.para" /> <br/> attr attribute para = <s:property value="#attr.para" /> <br/> <hr>
输出:
HTTP parameters para = hellorequest attribute para = request scope attribute
session attribute para = session scope attribute
application attribute para = aplication scope attribute
attr attribute para = request scope attribute
2. 通过OGNL表达式创建list集合,并且遍历出集合中的值
<br> <s:set name="list" value="{'eeeee','ddddd','ccccc','bbbbb','aaaaa'}"></s:set> <s:iterator value="#list" var="o"> ${o}<br/> <!--<s:property />--> </s:iterator> <br/> <hr>
输出:
eeeee
ddddd
ccccc
bbbbb
aaaaa
3. 通过OGNL表达式创建Map集合,并且遍历出集合中的值
<br> <s:set name="map" value="#{'1':'eeeee','2':'ddddd','3':'ccccc','4':'bbbbb','5':'aaaaa'}"></s:set> <s:iterator value="#map" var="o"> ${o.key}->${o.value}<br/> <!-- <s:property value="#o.key"/>-><s:property value="#o.value"/><br/> --> <!-- <s:property value="key" />-><s:property value="value" /> --> </s:iterator> <br/> <hr>
输出:
1->eeeee
2->ddddd
3->ccccc
4->bbbbb
5->aaaaa
4. 通过OGNL表达式,进行逻辑判断
<br> <s:if test="'aaa' in {'aaa','bbb'}"> aaa 在 集合{'aaa','bbb'}中; </s:if> <s:else> aaa 不在 集合{'aaa','bbb'}中; </s:else> <br/> <s:if test="#request.req not in #list"> 不 在 集合list中; </s:if> <s:else> 在 集合list中; </s:else> <br/> <hr>
输出:
aaa 在 集合{'aaa','bbb'}中;
不 在 集合list中;
5. 通过OGNL表达式的投影功能进行数据筛选
<s:iterator value="#attr.listStudent.{?#this.id % 2 != 0}" var="cur"> <s:property value="#cur.username"/> </s:iterator> <br/> <s:iterator value="#attr.listStudent.{^#this.id % 2 != 0}" var="cur"> <s:property value="#cur.username"/> </s:iterator> <br/> <s:iterator value="#attr.listStudent.{$#this.id % 2 != 0}" var="cur"> <s:property value="#cur.username"/> </s:iterator> <br/> <s:property value="#attr.listStudent.{username}[0]"/> <br/> <s:property value="#attr.listStudent.{$#this.id == 3}.{username}[0]"/> <br/>
输出:
张三 赵五 钱七
张三
钱七
张三
赵五
6. 通过OGNL表达式访问某个类的静态方法和值
<br> <s:property value="@java.lang.Maths@floor(32.56)" /> <br/> <hr>
输出:
32.0
7. OGNL表达式 迭代标签 详细
<br> <s:set name="list2" value="{'aa','bb','cc','dd','ee','ff','gg','hh','ii','jj'}"></s:set> <table border="1"> <tr> <td> 索引 </td> <td> 值 </td> <td> 奇? </td> <td> 偶? </td> <td> 首? </td> <td> 尾? </td> <td> 当前迭代数量 </td> </tr> <s:iterator value="#list2" var="o" status="s"> <tr bgcolor="<s:if test="#s.even">pink</s:if>"> <td> <s:property value="#s.getIndex()" /> </td> <td> <s:property /> </td> <td> <s:if test="#s.odd">Y</s:if> <s:else>N</s:else> </td> <td> <s:if test="#s.even">Y</s:if> <s:else>N</s:else> </td> <td> <s:if test="#s.first">Y</s:if> <s:else>N</s:else> </td> <td> <s:if test="#s.isLast()">Y</s:if> <s:else>N</s:else> </td> <td> <s:property value="#s.getCount()"/> </td> </tr> </s:iterator> </table>
输出:
package com.clzhang.ssh.demo6; import java.util.*; public class Student { private int id; private String username; private int grade; private Date birthday; //只要是重写一个类的构造方法,就必须要为这个类保留空的构造方法 //因为框架默认的都会去调用无参的空的构造方法 public Student(){}; public Student(int id, String username, Date birthday) { this.id = id; this.username = username; this.birthday = birthday; } public Student(String username,int grade){ this.username = username; this.grade = grade; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { //如果不重写它的toString()方法的话,默认调用toString()将输出【类型+@+内存地址的哈希值】 return "{学生姓名:" + username + ",成绩:" + grade + "}"; } }
View Code
更多阅读参考:
struts2中的OGNL表达式(转)struts2:遍历自定义字符串数组,遍历Action实例所引用对象中的数组
相关文章推荐
- 为何使用EL表达式能够访问valueStack中对象的属性、采用OGNL表达式创建List/Map集合对象
- S7.2_Struts2_OGNL OGNL表达式的投影和选择技术范例,OGNL表达式#和$符号的使用范例,以及值栈
- java关于集合(list,set,map)的遍历与增强for循环的使用
- Struts2 使用OGNL遍历map方法详解
- Struts2的OGNL遍历数组、List、简单的Map
- 不使用遍历循环、Linq与Lamba表达式快速对List集合进行筛选与排序——在集合类里使用Find、FindAll与Sort
- List和Map集合的使用及使用迭代器遍历数据
- Struts2的OGNL遍历数组、List、简单的Map
- OGNL表达式遍历List集合
- jsp 页面中用struts2 标签遍历集合(list map)
- Struts2使用OGNL表达式封装输入数据(直接封装Map类型)
- Struts2中ognl遍历数组,list和map方法详解
- Java关于集合(list,set,map)的遍历与增强for循环的使用
- Struts2 使用OGNL表达式投影(过滤)
- Struts2使用OGNL遍历各种map总结
- Struts2的OGNL遍历数组、List、简单的Map
- Struts2使用OGNL遍历各种map总结
- map集合的4种遍历方法和List集合的2种遍历和迭代器Iterator的使用
- Map集合与List集合混合使用对比
- 使用struts2中的ognl表达式调用类方法