Search code examples
javaspringjstl

JSTL iterate list


I have this class:

public class Orders{

    private Integer id;
    private String name;

    //getters/setters

}

In controller I pass a List<Orders> to jsp:

@RequestMapping(value = "/orders")
public ModelAndView orders(){

    List<Orders> orders = ...//get list from db

    //print list in console
    orders.forEach(e -> System.out.println(e.getId() + " - " + e.getName()));
    //print -> 1 - name1 ; 2 - name2

    return new ModelAndView("orders", "orders", orders);

}

In jsp use it like this:

${orders.size()}
<c:forEach items="${orders}" var="order">
    <c:out value="${order.getId()}"></c:out>
</c:forEach>

In browser at inspect(html code) looks like this:

"2"
<c:foreach items="[com.web.entity.Orders@21e16dd6, 
                   com.web.entity.Orders@52a33913]" var="order">
    <c:out value=""></c:out>
</c:foreach>

I tested in controller by printing list in console and everything is right.

Why in jsp is not printed?


Solution

  • Could you please provide more details (controller code, html page tags ...). Still I've some point to share with you :

    • Use always a toString method in your Entity/POJO.
    • Use order.id instead of order.getId()
    • Make sure you have JSTL core tag in the top of your HTML page :
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    • Try to have a simple c:out tag:
    <c:out value="${order.id}"/>