Foreach (spring) does not work in JSP

I am trying to make something easy, but it does not work. Am I missing 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 sysout without any problems. I can get the length of ${fn:length(listeApp)} , but I would like to use the Foreach function :)

Any tips? Thanks

+4
source share
2 answers

I found a solution: include this in JSP

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

I feel like an idiot :)

+5
source

In a test project, I did 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 completion purposes:

  • Make sure jstl is imported on JSP page
  • Make sure jstl jar is in classpath
  • Verify that the name used in the JSP matches the name used in the controller

With success. Hope this helps you!

+1
source

Source: https://habr.com/ru/post/1480106/


All Articles