1

My POJO has the following list of users and what I am trying to do is loop users and loop languages for each user

public class Display{ 
    private List<User> users;
}

class User{
    private name;
    private List<Language> languages;
}

class Language {
    private int langId;
    private String lang;
}

I tried the below code by creating a reference to Display class "disp" and retrieving the users list and looping it in foreach.but it didn't work. I was able to loop a list of objects but this scenario requires looping of a list inside another list.

My jsp

<c:forEach var="user" items="${disp.users}" begin="0" end="${fn:length(disp.users)-1}" step="1">
     <table>
       <tr> 
         <td> user.name </td>
       </tr>
     </table>
     <c:forEach var="language" items="${user.languages}" begin="0" 
     end="${fn:length(user.languages)-1}" step="1">
        <table>
         <tr> 
           <td> language.id</td>
         </tr>
       </table>
     </c:forEach>

</c:forEach>

How can I loop user list and languages list for each user? any help is appreciated. Thanks in advance

shmosel
  • 49,289
  • 6
  • 73
  • 138
Munni
  • 79
  • 1
  • 7
  • 2
    *it didn't work* is not a useful problem description. Please elaborate. – shmosel Aug 30 '17 at 21:10
  • Hi @shmosel I see below exception at 2nd forEach javax.el.ELException: Cannot convert [0>] of type [class java.lang.String] to [int] at org.apache.el.lang.ELSupport.coerceToNumber – Munni Aug 30 '17 at 21:24
  • I figured it out. Error was in the "end" parameter in forEach. – Munni Aug 30 '17 at 23:31

1 Answers1

1

Try this ( I've changed the 'id' to 'langId' too)

<c:forEach items="${disp.users}" var="user">
    <table>
       <tr> 
         <td> ${user.name} </td>
       </tr>
     </table> 
    <c:forEach items="${user.languages}" var="language">
    <table>
         <tr> 
           <td> ${language.langId}</td>
         </tr>
       </table>        
    </c:forEach>
</c:forEach>

Ref: How to iterate an ArrayList inside a HashMap using JSTL?