简体   繁体   中英

Iterate two Lists in struts2

I have two lists, selectedFields which contains the headers (eg: id , username , firstname ) and custDetails which contains customer details(eg: id , username , firstname , birthdate , address etc.). what I want to do here is print only the data from custDetails in <td> which are contained in selectedFields .if selectedFields contains username then only the username values will be shown like <td>John</td> and header will be like <th>username</th> .

<table>
<thead>
  <!-- Selected Fields contains id,username -->
  <s:iterator value="selectedFields">                                           
  <th><s:property/></th>
  </s:iterator>
</thead>
<tbody>
  <!-- custDetails contains id,username,firstname,lastname,address,city,state etc. -->
  <s:iterator value="custDetails" status="custDetails">
    <tr>
    <s:iterator value="selectedFields" status="selectedFields">
    <!-- Display only id,username columns from custDetails since selectedFields contains these values -->  
    <td><s:property value="<s:property />"/></td>
    </s:iterator>
    </tr>
  </s:iterator>                               
</tbody>
</table>

You can use if and else tags to choose which columns you want to display. Unless you don't want to iterate the object properties.

<s:iterator var="sf" value="selectedFields">
  <!-- Display only id,username columns from custDetails since selectedFields contains these values --> 
  <s:if test="#sf == 'id'"> 
   <td><s:property value="id"/></td>
  </s:if>
  <s:else>
  <s:if test="#sf == 'username'"> 
   <td><s:property value="username"/></td>
  </s:if>
  </s:else>
</s:iterator>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM