假设我有两张桌子,星期一和星期二。我想根据对象的属性填充这些表。因此:
<s:iterator value="trainings">
<table>
<tr><th>Monday</th></tr>
<s:if test="weekday.toString().equals('MONDAY')">
<tr>
<td>
<s:property value="title" />
</td>
</tr>
</s:if>
</table>
<table>
<tr><th>Tuesday</th></tr>
<s:if test="weekday.toString().equals('TUESDAY')">
<tr>
<td>
<s:property value="title" />
</td>
</tr>
</s:if>
</table>
</s:iterator>
如何正确使用此功能?谢谢
编辑:
public class Training {
private Weekday weekday;
....
}
public Enum Weekday {
MONDAY,
TUESDAY,
...
}
答案 0 :(得分:1)
要从单个源填充两个不同的表(在操作中应该更好地分割服务器端),通常需要迭代集合两次:
<table>
<tr><th>Monday</th></tr>
<s:iterator value="trainings">
<s:if test="weekday.toString().equals('MONDAY')">
<tr><td><s:property value="title" /></td></tr>
</s:if>
</s:iterator>
</table>
<table>
<tr><th>Tuesday</th></tr>
<s:iterator value="trainings">
<s:if test="weekday.toString().equals('TUESDAY')">
<tr><td><s:property value="title" /></td></tr>
</s:if>
</s:iterator>
</table>
为避免多次执行<s:if>
,您可以使用OGNL's List Selection:
<table>
<tr><th>Monday</th></tr>
<s:iterator value="trainings.{? #this.weekday.toString() == 'MONDAY' }">
<tr><td><s:property value="title" /></td></tr>
</s:iterator>
</table>
<table>
<tr><th>Tuesday</th></tr>
<s:iterator value="trainings.{? #this.weekday.toString() == 'TUESDAY' }">
<tr><td><s:property value="title" /></td></tr>
</s:iterator>
</table>
会产生更小更干净的HTML代码。