一.OGNL常用符号(接上一篇文章):
1.#号
1)<s:property value="#request.username"/> 作用于struts2的域对象,而不是普通域对象
2)<s:property value="#user.username"/>作用于JavaBean对象
3)<s:property value="#username"/>作用于普通字符串,如果value只是一个字符串的话,不是JavaBean对象,可以加#,也可以不加.
4) ?#,表示所有记录
^#,表示第一条记录
$#,表示最后一条记录
{?#条件}[n],表示第n+1条记录,
例如:<s:iterator var="user" value="#attr.userList.{$#this.age>=22}">
<s:iterator var="user" value="#attr.userList.{?#this.age>=22}[1]">
取出第N个记录,N为索引号
实例:ognl_3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <%@ page import="java.util.*"%> <%@ page import="ognl.User"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% List<User> userList = new ArrayList<User>(); userList.add(new User(1, "张三", 20)); userList.add(new User(2, "李四", 25)); userList.add(new User(3, "amos", 30)); userList.add(new User(4, "hi_amos", 28)); pageContext.setAttribute("userList", userList); %> <hr> <table border="1" align="center" width="50%"> <caption><font color="blue">查询大于等于25岁的所有用户,?#</font></caption> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> </tr> <!-- {条件},{}中存放条件;this表示该个对象或该条数据;?表示全部符合条件的数据;^表示查询符合条件的第一个数据--> <s:iterator var="user" value="#attr.userList.{?#this.age>=25}"> <tr> <td><s:property value="#user.id" /></td> <td><s:property value="#user.name" /></td> <td><s:property value="#user.age" /></td> </tr> </s:iterator> </table> <br><hr><br> <table border="1" align="center" width="50%"> <caption><font color="blue">查询大于等于25岁的第一个用户,^#</font></caption> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> </tr> <!-- {条件},{}中存放条件;this表示该个对象或该条数据;?表示全部符合条件的数据;^表示查询符合条件的第一个数据--> <s:iterator var="user" value="#attr.userList.{^#this.age>=25}"> <tr> <td><s:property value="#user.id" /></td> <td><s:property value="#user.name" /></td> <td><s:property value="#user.age" /></td> </tr> </s:iterator> </table> <br><hr><br> <table border="1" align="center" width="50%"> <caption><font color="blue">查询大于等于25岁的最后一个用户,$#</font></caption> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> </tr> <!-- {条件},{}中存放条件;this表示该个对象或该条数据;?表示全部符合条件的数据;^表示查询符合条件的第一个数据--> <s:iterator var="user" value="#attr.userList.{$#this.age>=25}"> <tr> <td><s:property value="#user.id" /></td> <td><s:property value="#user.name" /></td> <td><s:property value="#user.age" /></td> </tr> </s:iterator> </table> <br><hr><br> <table border="1" align="center" width="50%"> <caption><font color="blue">查询大于等于25岁的最后一个用户,{?#}[索引值]</font></caption> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> </tr> <!-- {条件},{}中存放条件;this表示该个对象或该条数据;?表示全部符合条件的数据;^表示查询符合条件的第一个数据--> <s:iterator var="user" value="#attr.userList.{?#this.age>=25}[1]"> <tr> <td><s:property value="#user.id" /></td> <td><s:property value="#user.name" /></td> <td><s:property value="#user.age" /></td> </tr> </s:iterator> </table> </body> </html>