Search code examples
listjspjstlelscriptlet

Passing List from controller to view with JSTL or EL


I would like to switch from scriplets to EL or JSTL, yet i simply cannot figure out how you hve to pass your attributes from the controllers to the view.

Lets say we have a list of cars in our controller, and we want to display them on our view. With scriplets we would do:

<% List<Car> cars= (List) request.getAttribute("Cars"); %>

To retrieve the list from our controller to our view.

How is this done with EL or JSTL ?


Solution

  • I am assuming you are setting your list as follows from your controller -

    request.setAttribute("items", items);
    

    If this is the case, then you can access each item of this list as follows -

    <c:forEach var="item" items="${items}">
    .......
    </c:forEach>
    

    If the list contains objects which have a property called, say, price (which means you have getPrice getter in the class), then you can do this to access that value -

    ${item.price}
    

    inside that jstl loop.