25.OGNL与ValueStack(VS)-集合对象进阶

转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html

首先在LoginAction中增加如下字段并提供相应的get/set方法:
private List studentList = new ArrayList();
然后再在execute中为其初始化赋值,代码如下:
studentList.add(new Student("jack", 20, 86.0f));

studentList.add(new Student("lily", 22, 96.5f));

studentList.add(new Student("tom", 23, 56.5f));

最后在loginSuc.jsp中增加如下代码:
获取List中的Student对象:<s:property value="studentList"/><br>

利用投影获取List中的name属性:<s:property value="studentList.{name}"/><br>

利用投影获取List中的age属性:<s:property value="studentList.{age}"/><br>

利用投影获取List中的第一个对象的name属性:<s:property value="studentList.[0]{name}"/>   或者<s:property value="studentList.{name}[0]"/><br>

利用选择获取List中grade>60的student信息:

<s:property value="studentList.{?#this.grade>60}"/><br>

利用选择获取List中grade>60的student名字信息:

<s:property value="studentList.{?#this.grade>60}.{name}"/><br>

利用选择获取List中grade>60的第一个student名字信息:

<s:property value="studentList.{?#this.grade>60}.{name}[0]"/><br>

利用选择获取List中grade>60的第一个student名字信息(链表):

<s:property value="studentList.{^#this.grade>60}.{name}"/><br>

利用选择获取List中grade>60的最后一个student名字信息(链表):

<s:property value="studentList.{$#this.grade>60}.{name}"/><br>

说明:这里重点是说明?#的使用,结合此例来看,studentList中有许多Stutdent对象,我们可以用条件来限制取哪些对象,这些条件必须以?#开始,并且条件要用{}括起。而this是指在判断studentList中的对象是否符合条件的当前对象。?#是指取出符合条件的所有Student对象,而^#是指取出符合条件的第一个对象,$#是指取出符合条件的最后一个对象。

原文地址:https://www.cnblogs.com/sharpest/p/5582882.html