嗨,我设法通过使用JSTL forEach循环在我们的主页上显示数据库中的产品,一切正常。(在这种情况下我们的关于页面)然后返回主页,我认为这很可能是由于某些错误,并且在刷新homePage.jsp时没有再次调用doGet方法(虽然这可能是完全错误的,但我对Web开发很陌生)
无论如何,我注意到我的小组没有创建web.xml文件,并这样做了,这似乎解决了这个问题。
现在有一个产品显示,即使我离开主页并返回它,它也总是显示。然而,似乎出了问题,因为只有一个“产品”显示(数据库中有7个),并且名称没有显示,而是${productL.getProductName()}“打印在它的位置。
我已经尝试了一些不同的解决方案,例如使用` Package 产品的创建,并<jsp:include page="/TestServlet" />在homePage.jsp中使用,但它没有解决这个问题,我现在很迷茫,任何关于为什么在创建web.xml之前循环和检索products/productNames的帮助都将非常感激。
TestServlet.java
package org.ics.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
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 org.ics.ejb.ProductType;
import org.ics.facade.FacadeLocal;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
@EJB
FacadeLocal facade;
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<ProductType> productTypeList = facade.getAllProducts();
// Set the list of product names as a request attribute
request.setAttribute("productTypeList", productTypeList);
// Forward the request to the homePage.jsp page
RequestDispatcher dispatcher = request.getRequestDispatcher("/homePage.jsp");
dispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
homePage.jsp(仅显示产品的部分)
<section id="our-products" data-section="our-products"
class="section-padding">
<h1>Our products</h1>
<%
List<ProductType> productTypeList = (List<ProductType>) request.getAttribute("productTypeList");
%>
<c:forEach items = "${productTypeList}" var="productL">
<div class="product">
<img src="lemon.jpg" alt="Lemon">
<p>${productL.getProductName()}"</p>
<div class="quantity">
<button class="decrement">-</button>
<input type="number" value="0" min="0" max="10"
class="quantity-input">
<button class="increment">+</button>
</div>
<div class="product-details" data-product-id="testId">
<button class="add-to-cart-btn">Add to Cart</button>
</div>
</div>
</c:forEach>
</section>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>org.ics.servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
A picture of the current display of products(目前我们不使用图片,我们只有一个占位符文本说柠檬)
1条答案
按热度按时间k5hmc34c1#
我愚蠢地链接到jsp而不是servlet,这使得不可能从servlet加载产品,谁会想到呢?