2012-04-27 55 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