MinionKing
MinionKing

Reputation: 187

Foreach(spring) doesn't work in JSP

I try to do something easy, but it's not working. Do i miss something obvious ?

my jsp :

<c:if test="${not empty listeApp}">
<table border = "1">
    <c:forEach var="apps" items="${listeApp}" >
        <tr>
            <td>${apps.ID_APPLICATION}</td>
            <td>${apps.ID_APPLICATION}</td>

        </tr>
    </c:forEach>
</table>

my controller :

public ModelAndView portail() {

        applicationDao appDAO = new applicationJPADaoImpl();
        return new ModelAndView("portail", "listeApp", appDAO.listeAll());

}

Nothing is displayed.

${listeApp[0].ID_APPLICATION} works.

my list is good, i printed it in the sysout without any prob. i can get the lenght ${fn:length(listeApp)} but i would like to use the Foreach feature :)

Any advice ? Thanks

Upvotes: 4

Views: 6848

Answers (2)

Olimpiu POP
Olimpiu POP

Reputation: 5067

On a test project I made the following:

Controller code:

@RequestMapping(value = "{id}", method = RequestMethod.GET)
public String getView(@PathVariable Long id, Model model) {
    List<Menu> menus = menuService.findAllMenus();

    model.addAttribute("menus", menus);

    return "menu/view";
}

JSP Code:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
   <head>
     <title>Title in Work!</title>
   </head>
   <body>
     <table style="align:center;">
       <th>Name</th>
       <th>Price</th>
       <th>Restaurant</th>
       <c:forEach items="${menus}" var="menu">
         <tr>
            <td><c:out value="${menu.name}"/></td>
            <td><c:out value="${menu.price}"/></td>
            <td><c:out value="${menu.restaurant.name}"/></td>
        </tr>
    </c:forEach>
</table>

EDIT

For completions sake:

  1. Ensure the jstl is imported in the JSP page
  2. Ensure that the jstl jar is in the classpath
  3. Ensure that the name used in the JSP is the same as the name used in the controller

With success. Hope it helps you!

Upvotes: 1

MinionKing
MinionKing

Reputation: 187

I found the solution : include this in JSP

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

I feel like an idiot :)

Upvotes: 5

Related Questions