Home > Back-end >  <c:if test = "${product.categoryid}==${loop1.index 1}"> does not seem to work [dupli
<c:if test = "${product.categoryid}==${loop1.index 1}"> does not seem to work [dupli

Time:09-17

I have two array lists, a category and a product array list, in my servlet

@WebServlet(urlPatterns = "/homepage.do")
public class HomePageServlet extends HttpServlet {
    Category category = new Category(0, null, null);
    CategoryDAO cd = new CategoryDAOImpl();
    
    Product product = new Product(0, null, null, null);
    ProductDAO pd = new ProductDAOImpl();
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                
        List<Category> categories = cd.selectAllCategory();
        request.setAttribute("categories", categories);
        System.out.println(categories);
        
        List<Product> products = pd.selectAllProducts();
        request.setAttribute("products", products);
        System.out.println(products);
        
            request.getRequestDispatcher("/WEB-INF/views/homepage.jsp").forward(request, response);
            
    
    }

}

And the JSP file

    <c:forEach items="${categories}" var="category" varStatus="loop1">
    <div class="category-block" id ="${loop1.index 1}">
        <h2 class = "category-title">${category.name } </h2>
        <a href="#">Read description</a><br>
        <a href="/delete-category.do?categoryid=${loop1.index 1}" class="btn btn-danger">Delete ${category.name} category</a>&nbsp; &nbsp; &nbsp;
        <a href="/edit-category.do?categoryid=${loop1.index 1}" class="btn btn-warning">Edit ${category.name} category</a><br><br>
       
        <div class="row ">
            <c:forEach items="${products}" var="product" varStatus="loop2">
            <c:if test = "${product.categoryid}==${loop1.index 1}">
                <div class="col-sm-3">
                    <div class="card border w-60 text-center"id ="${loop2.index}" >
                        <img class="card-img" src="" alt="some image">${product.image }
                        <p id="${loop2.index}" class="card-title">${product.name}</p>
                        <p class="card-text">${product.description }</p>
                        <a href ="/edit-product.do?categoryid=${loop1.index 1}&productid=${loop2.index 1}" class="btn btn-success">Edit</a>&nbsp; &nbsp; &nbsp;
                        <a href ="/delete-product.do?name=${product.name}&productid=${loop2.index}" class="btn btn-danger">Delete</a>
                        
                    </div>
                   </div>
               </c:if>
            </c:forEach>
        </div>  
        <a href="/add-product.do?categoryid=${loop1.index 1}" class="btn btn-primary">Add    Product</a>
    </div>
 </c:forEach>

I want that whenever the id of the first loop (loop1.index 1) matches the product.categoryid, that product(with that categoryid) should display.

Without the if taglib, the products display in all the categories irrespective of the id. With the if tag however, no product displays because the first product has categoryid = 2.

The aim of the display is that, for each number of the first loop (loop1.index 1), each product should be checked to see if its product.categoryid == loop (loop1.index 1). This is done for all the product and those with a true condition are printed, and then the first loop goes on for the next id. Please, any help?

CodePudding user response:

I think you should replace the c:if as follows:

<c:if test = "${product.categoryid == loop1.index 1}">
  • Related