2012-04-27 148 views
0

的內容我在jsp的一個小問題用foreachJSP的foreach犯規打印列表

我試圖在jstl/jsp - iterating over a vector of beans做的一切都像,但我不能得到任何印刷我的屏幕

這裏是一些代碼:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Shop products</h1> 
     <table> 
      <c:forEach var="item" items="${products}"> 
       <tr> 
        <td> 
         <c:out value="${item.name}"/> 
        </td> 
       <td> 
        <c:out value="${item.amount}"/> 
       </td> 
       </tr> 
      </c:forEach> 
     </table> 
    </body> 
</html> 

我的servlet的processRequest():

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    products = new LinkedList<ProductHandler>(); 
    products.add(new ProductHandler("A", 5)); 
    products.add(new ProductHandler("B", 10)); 
    products.add(new ProductHandler("C", 7)); 
    request.setAttribute("products", products); 
    getServletContext().getRequestDispatcher("/JSP/shop.jsp").forward(request, response); 
} 

而ProductHandler:

public class ProductHandler { 
    private String name; 
    private int amount; 

    public ProductHandler() { 
     name = null; 
     amount = 0; 
    } 

    public ProductHandler(String name, int amount) { 
     this.name = name; 
     this.amount = amount; 
    } 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * @return the amount 
    */ 
    public int getAmount() { 
     return amount; 
    } 

    /** 
    * @param amount the amount to set 
    */ 
    public void setAmount(int amount) { 
     this.amount = amount; 
    } 

} 

標識是指出我沒有犯錯

回答

4

嘗試加入這行來shop.jsp

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

頂部我假設您已經閱讀HTTP非常感激:/ /stackoverflow.com/tags/jstl/info – rickz 2012-04-27 23:21:20