Virtual Realist
Virtual Realist

Reputation: 175

How to iterate the list of objects in JSP using jstl and EL?

I am passing a List from my Spring MVC controller class to my jsp page. This list contains multiple objects ie., List. I need to iterate these objects to get values. How to iterate values using jstl and EL (not using simple for loop). My code in jsp file is...

  <c:forEach items="${myList}" var="allEmp"> //'myList' is the list that I passing from my controller class. ie., List<Employee>
     //after iterating list, it will return Employee object.From this employee object, I want to iterate the values
      <tr>
         <td><c:out value="${allEmp.employee.getEmpId()}"/></td>
         <td><c:out value="${allEmp.employee.getFirstName()}"/></td>
         <td><c:out value="${allEmp.employee.getLastName()}"/></td>
      </tr>
    </c:forEach>

I know my code is wrong. How to iterate from Employee object. Do i need another for each loop to iterate employee object? Please help me out.

Upvotes: 1

Views: 3535

Answers (2)

Nimesh
Nimesh

Reputation: 802

Only change as below:

     <td><c:out value="${allEmp.employee.empId}"/></td>
     <td><c:out value="${allEmp.employee.firstName}"/></td>
     <td><c:out value="${allEmp.employee.lastName}"/></td>

Upvotes: 6

Subler
Subler

Reputation: 657

If what youre saying is you want to iterator over the fields of an Employee... That seems like a very strange thing to do, and I dont think it can be done just like that... But you could add a function to Employee returning a list of Strings and then iterate over that list with another loop... but that is probably just as much (if not more) work than just writing it out like u have done already

Upvotes: 0

Related Questions