0

我对jsp中的foreach有一个小问题

我尝试像在jstl/jsp 中那样做所有事情——迭代一个 bean 的向量,但我无法在我的屏幕上打印任何东西

这是一些代码:

<%@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 进程请求():

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);
}

和产品处理程序:

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

1 回答 1

4

尝试将此行添加到 shop.jsp 的顶部

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>   
于 2012-04-27T23:19:43.633 回答