2010-12-23 18 views
1

我編程在Java中,我有我想JSTL迭代檢查是否有其他元素有一定的相似性與當前對象的列表。我想在我的jsp中這樣做,因爲它只是一個顯示問題。如何在一個JSTLç重複列屬性組行:的forEach

讓我們假設一個例子,並說我的對象有三個屬性:id,lastname和firstname。 id是家庭的標識符,我想在HTML表格中顯示列表,但是在迭代我的列表時我想檢查列表的其餘部分以查看是否有其他家庭成員在場,以便我可以在一個td標記中重新組合它們。

 
1 | TOTO | James 
2 | FOE | Cameron 
2 | FOE | Jessica 
1 | TOTO | Pat 
3 | SAMPLE | Bob 

預期結果:

<table> 
    <tr><td>1</td><td>TOTO</td><td>James, Pat</td></tr> 
    <tr><td>2</td><td>FOE</td><td>Cameron, jessica</td></tr> 
    <tr><td>3</td><td>SAMPLE</td><td>Bob</td></tr> 
</table> 

再次,這是一個基本的例子,你可能會告訴我重新集結我的家庭在其他物體在我的模型層,但我寧願不要要做到這一點。


編輯:我暗示while循環,因爲我的名單是由ID排序,所以我可以很容易地查了下元素。但其他解決方案對我來說會很好。

+0

你的例子中的列表看起來不是我命令的。 – dogbane 2010-12-23 10:52:07

回答

0

您可以在Map和攔截上得到處理lastnames的保持。

<table> 
    <jsp:useBean id="processed" class="java.util.HashMap" /> 
    <c:forEach items="${persons}" var="person"> 
     <c:if test="${empty processed[person.lastName]}"> 
      <c:set target="${processed}" property="${person.lastName}" value="true" /> 
      <tr> 
       <td>${person.familyId}</td> 
       <td>${person.lastName}</td> 
       <td>${person.firstName} 
        <c:forEach items="${persons}" var="other"> 
         <c:if test="${person.lastName == other.lastName and person.firstName != other.firstName}"> 
          , ${other.firstName} 
         </c:if> 
        </c:forEach> 
       </td> 
      </tr> 
     </c:if> 
    </c:forEach> 
</table> 

但我同意,這應該在(數據)模型方面完成,而不是在視圖方面。

0

您的bean應該執行分組,而不是JSP。

相關問題