Help with forEach in jsp

0

I'm trying to make an if in a jsp file, but I can not print all the values that are in the products table. For some reason my code only prints the first product and in the table I have 3 products.

            <c:forEach items="${products}"  var="product">
        <div class="w3-col l3 s4" style="width: 25rem;">
            <div class="w3-container">
            <img src="/w3images/" style="width:80%">
                ${product.productName}

            </div>
            <div class="w3-container">
                <h5 class="card-title">${product.productPrice}</h5>
                <p class="card-text">${product.productDescription}</p>
                <input id="productId" name="productId" type="hidden" value="${product.productId}">
                <button type="submit" class="w3-button w3-black" >Bid</button>

                <br>
            </div>
        </div>
</c:forEach>

My backend:

    private ProductService productService = new ProductService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String product_name = request.getParameter("userSearch");

        List<Product> productList = productService.searchProductName(product_name); //get product by name

        request.setAttribute("products", productList);

        request.getRequestDispatcher("/WEB-INF/views/userAds.jsp").forward(request, response);

        System.out.println(productList);


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String userSearch = request.getParameter("userSearch");


        List<Product> productList = productService.searchProductName(userSearch); //get product by name

        request.setAttribute("products", productList);

        request.getRequestDispatcher("/WEB-INF/views/userAds.jsp").forward(request, response);

    }   

}
    
asked by anonymous 11.09.2018 / 21:55

1 answer

0

Your forEach seems to be okay, does not the problem be in the backend? Maybe your 'products' object is not being populated with all three items.

Debug your Java code to check this, or dump the object in the JSP like this:

<!--
  ${products}
-->

I believe this will show the content of the object (calling the toString) as a comment in the generated html, so you can check by clicking 'View Source' in the browser.

In any case, it would be interesting to put the backend code to help with the analysis.

    
11.09.2018 / 22:11