2013-05-10 155 views
4

我嘗試做一些簡單的事情,但它不起作用。我想念一些明顯的東西嗎?Foreach(spring)在JSP中不起作用

我的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> 

我的控制器:顯示

public ModelAndView portail() { 

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

} 

沒有。

${listeApp[0].ID_APPLICATION}的作品。

我的清單很好,我在沒有任何問題的情況下將它打印在sysout中。 我可以得到長度${fn:length(listeApp)}但我想使用Foreach功能:)

有什麼建議嗎? 感謝

+0

您是否嘗試在foreach中僅打印文本?看它是否迭代? – 2013-05-10 12:38:08

+0

它沒有。以及它僅打印一旦.. \t \t \t \t測試 \t \t \t =>結果:測試 – MinionKing 2013-05-10 12:40:34

回答

5

我找到了解決辦法: 包括這JSP

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

我覺得自己像個白癡:)

1

在測試項目中,我做了如下:

控制器代碼:

@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代碼:

<%@ 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> 

編輯

完井緣故

  1. 確保JSTL在JSP頁面中輸入
  2. 確保JSTL的jar在類路徑
  3. 確保JSP中使用的名稱與控制器中使用的名稱相同

成功。希望它可以幫助你!