JSTL forEach标签在jsp中什么也不显示

时间:2019-05-05 02:15:04

标签: jsp servlets jstl

我在我的jsp中使用jstl,并且我的jsp的所有元素都在显示,除了JSTL forEach标记之外,放在此循环中的所有内容都将消失。

这是我的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>

这是我的Servlet文件:

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




    }
}

我在另一个jsp中具有完全相同的条件,并且可以正常工作,但是我不明白为什么会有问题。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您需要检查您的网址是否包含查询字符串yoururl?action=best

需要检查List<ItemClass> best = query3.getResultList();中的最佳值是否为空或为空

如果满足两个条件,则必须显示最佳状态。