So, I've been trying to figure this out for quite while, basically I am making webshop in Java(for college), and what I want to do is to display data from DB on my front page or as in dropdown menu. But it doesnt work until I press on link which has same data, and when I return to page I previously wanted to display in, it shows.
Here is mine JSP and Servlets:
<div class="row">
<c:forEach items="${products}" var="item">
<kova:test
product="${item}"></kova:test>
</c:forEach>
</div>
***
***
<%@tag description="put the tag description here" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@attribute name="product" type="model.Product"
required="false" %>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
<div class="card-body">
<h4 class="card-title">
<%--<form action="product" method="post">
<input type="hidden" name="idProduct" value="${product.productId}"/>
<button type="submit" id="btn" class="btn btn-info" >View</button>
</form>--%>
<a href="#">${product.title}</a>
</h4>
<h5>${product.price} kn</h5>
<p class="card-text">${product.about}</p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
IRepo iRepo = RepoFactory.getRepo();
req.getSession().setAttribute("products",
iRepo.getAllProducts());
RequestDispatcher rd =
getServletContext().getRequestDispatcher(
"/products.jsp");
rd.forward(req, resp);
}
And here is the jsp that works if I click its link:
<div class="row">
<h1 id="h1">All products</h1>
<div class="row products">
<ul>
<c:forEach items="${products}" var="item">
<li><kova:test
product="${item}"></kova:test></li>
<hr>
</c:forEach>
</ul>
</div>
</div>