如何使用<s:iterator>?</s:iterator>从单个Collection中绘制多个HTML表

时间:2015-03-04 08:48:01

标签: java html collections struts2 ognl

假设我有两张桌子,星期一和星期二。我想根据对象的属性填充这些表。因此:

<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,
    ...        
}

1 个答案:

答案 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代码。