Search code examples
jspservletsjstl

JSTL forEach tag displays nothing in jsp


I'm using jstl in my jsp and All of the elements of my jsp are displaying except JSTL forEach tag and everything that I put in this loop, will be disappear.

It's my jsp:

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Details</title>

    <base href="${pageContext.request.contextPath}/">

    <link href="https://fonts.googleapis.com/css?family=Arvo" rel="stylesheet">
    <meta name="description"
          content="A free shopping cart system. OpenCart is an open source">
    <link href="Resources/bootstrap.css" rel="stylesheet">
    <link href="Resources/stylesheet.css" rel="stylesheet">
    <link href="Resources/font-awesome.min.css" rel="stylesheet">
    <link href="Resources/bootstrap-datetimepicker.min.css" type="text/css" rel="stylesheet" media="screen">
    <script type="text/javascript" async="" src="Resources/analytics.js.download"></script>
    <script src="Resources/jquery-1.12.2.min.js.download"></script>
    <script src="Resources/bootstrap.min.js.download"></script>
    <script src="Resources/moment.js.download" type="text/javascript"></script>
    <script src="Resources/bootstrap-datetimepicker.min.js.download" type="text/javascript"></script>
    <script src="Resources/common.js.download"></script>

    <script async="" src="Resources/js"></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }

        gtag('js', new Date());

        gtag('config', 'UA-1988725-1');
    </script>
</head>
<body>

<header>
    <jsp:include page="header.jsp"/>
</header>
<div id="common-home">
    <div id="hero">
        <div class="container" style="height: 150px;">
            <div class="row">

                <jsp:include page="searchbar.html"/>
            </div>
            <div class="hidden-md hidden-lg"><img src="Resources/hero-image.png"
                                                  alt="The best FREE and open-source eCommerce platform"
                                                  title="The best FREE and open-source eCommerce platform"
                                                  class="img-responsive"></div>
        </div>
        <div class="device hidden-xs hidden-sm"><img src="Resources/hero-image.png"
                                                     alt="The best FREE and open-source eCommerce platform"
                                                     title="The best FREE and open-source eCommerce platform"
                                                     class="hidden"></div>
    </div>
    <div id="support" class="container text-center" style="padding-top: 30px">
        <div class="row">

            <div class="col-sm-6" style="margin-left:300px">

                <c:forEach items="${best}" var="item" >

                    <div style="text-align: center"><h2 style="display: inline-block; white-space: nowrap">${item.name}</h2></div>

                    <img src="Resources/images/${item.imageUrl}" alt="Dedicated Support"
                         title="Dedicated Support">

                    <p style="margin-bottom: 0px">Author: ${item.author}</p>
                    <p>Price : ${item.price}</p>
                </c:forEach>

                    <p>
                        <a href="https://dedicated.opencart.com/" target="_blank" class="btn btn-primary btn-lg hidden-xs">Add To Cart</a>
                        <a href="https://dedicated.opencart.com/" target="_blank"
                           class="btn btn-primary btn-lg btn-block visible-xs">Add To Cart</a>
                    </p>

            </div>
        </div>
    </div>

</div>
<jsp:include page="footer.html"/>

<script type="text/javascript"><!--
if (typeof (Zenbox) !== "undefined") {
    Zenbox.init({
        dropboxID: "20269606",
        url: "https://opencart.zendesk.com",
        tabTooltip: "Support",
        tabImageURL: "https://assets.zendesk.com/external/zenbox/images/tab_support.png",
        tabColor: "black",
        tabPosition: "Left"
    });
}
//--></script>

</body>
</html>

and This is My Servlet file :

package servlets;

import entities.CategoryClass;
import entities.ItemClass;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CategoryServlet",urlPatterns = "/foo/*")
public class CategoryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

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

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("NewPersistenceUnit");

        EntityManager entityManager = entityManagerFactory.createEntityManager();

        Query query = entityManager.createQuery("SELECT c FROM CategoryClass c");

        List<CategoryClass> categories = query.getResultList();

        request.setAttribute("categories",categories);

        String url = "/shopindex.jsp" ;
        String action = request.getParameter("action");

        if(action != null){

            // age action  null nis ,

            if (action.equals("search")){

                String keyword = request.getParameter("searchedword");

                Query query1 = entityManager.createQuery("from ItemClass where name like '%" + keyword + " %'"  );

                List<ItemClass> items = query1.getResultList();

                request.setAttribute("items",items);

                url = "/search.jsp" ;
            }

            if (action.equals("itemDetails")){

                String productId = request.getParameter("id");

                Query query2 = entityManager.createQuery("from ItemClass where id="+productId);

                List<ItemClass> itemThatWeWantedTheirDetails = query2.getResultList();

                request.setAttribute("itemThatWeWantedTheirDetails",itemThatWeWantedTheirDetails);

                url = "/itemDetails.jsp";

            }

            if (action.equals("best")){
                Query query3 = entityManager.createQuery("select i.name from ItemClass i where i.bought = (select max(ii.bought) from ItemClass ii )");
                List<ItemClass> best = query3.getResultList();

                request.setAttribute("best",best);


                int size = best.size();
                request.setAttribute("size1",size);

                url = "/besty.jsp";
            }


        }


        request.getServletContext().getRequestDispatcher(url).forward(request,response);

        entityManager.close();
        entityManagerFactory.close();




    }
}

I have exactly same condition in another jsp and that works fine but I don't understand why this have problem.

I will appreciate your help.


Solution

  • You need check your url contain query string yoururl?action=best or not

    And need check the best is null or empty in List<ItemClass> best = query3.getResultList();

    If two condition meets, the best must be shown.