<c:if test = "${product.categoryid}==${loop1.index+1}"> 好像不行

<c:if test = "${product.categoryid}==${loop1.index+1}"> does not seem to work

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

}

和 JSP 文件

    <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>

我希望只要第一个循环的 ID (loop1.index+1) 与 product.categoryid 匹配,就应该显示该产品(具有该类别 ID)。

如果没有 if 标签库,产品将显示在所有类别中,而不管 id。但是,如果使用 if 标签,则不会显示任何产品,因为第一个产品的 categoryid = 2.

显示的目的是,对于第一个循环(loop1.index+1)的每个数字,应检查每个产品以查看其 product.categoryid == 循环(loop1.index+1).这对所有产品都完成了,并且打印了具有真实条件的产品,然后第一个循环继续下一个 id。 请问有什么帮助吗?

我认为你应该将 c:if 替换如下:

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