struts2:OGNL表达式,遍历List、Map集合;投影的使用详解编程语言

OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。

片头(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=hello

1. 通过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 = hello
request 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="@[email protected](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>

输出:

struts2:OGNL表达式,遍历List、Map集合;投影的使用详解编程语言

8. 完整代码(ognl.jsp)

struts2:OGNL表达式,遍历List、Map集合;投影的使用详解编程语言

<%@ page language="java" import="java.util.*,com.clzhang.ssh.demo6.Student" pageEncoding="utf-8"%> 
<%@ taglib uri="/struts-tags" prefix="s"%> 
<% 
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); 
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<body> 
1. 通过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> 
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> 
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> 
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> 
5. 通过OGNL表达式的投影功能进行数据筛选 
<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: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/> 
<hr> 
6. 通过OGNL表达式访问某个类的静态方法和值 
<br> 
<s:property value="@[email protected](32.56)" /> 
<br/> 
<hr> 
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> 
</body> 
</html>

Student.java(只用到部分方法)

struts2:OGNL表达式,遍历List、Map集合;投影的使用详解编程语言

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 + "}"; 
} 
}

 

更多阅读参考:

struts2中的OGNL表达式(转)

struts2:遍历自定义字符串数组,遍历Action实例所引用对象中的数组

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/7971.html

(0)
上一篇 2021年7月18日
下一篇 2021年7月18日

相关推荐

发表回复

登录后才能评论