Answer the question
In order to leave comments, you need to log in
How to make a carousel of cards with ForEach via Bootstrap 5?
In general, such a thing is that I need to make a carousel on the website of a fictitious store.
Adding individual products to the cards by yourself is not an option, because. in the future, it will be necessary to make a recommendation of goods to the user. Therefore, it is necessary that the cards are added automatically to the carousel.
There is a "View Products" page, in which, when a new product is added, a product card is added through the database.
Here is a photo :
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<h3 class="w-100 my-5 text-center">Список товаров</h3>
<div class="w-100 d-flex justify-content-center m-2">
<c:forEach var="product" items="${listProducts}">
<div class="card m-2" >
<img src="insertCover/${product.cover.path}" class="card-img-top" alt="..." style="max-width: 12rem; max-height: 15rem; margin: 0 auto">
<div class="card-body">
<h5 class="card-title">${product.title} ${product.model}</h5>
<p class="card-text">${product.price}</p>
<a href="#" class="btn" style="background-color: #EB984E">Смотреть</a>
<a href="buyProductForm" class="btn" style="background-color: #EB984E">Купить</a>
</div>
</div>
</c:forEach>
</div>
Answer the question
In order to leave comments, you need to log in
Found a solution.
Yes its implementation, you need to create a Servlet
package servlets;
import entity.Product;
import jakarta.ejb.EJB;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import session.ProductFacade;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "CarouselServlet", urlPatterns = {
"/carousel",
})
public class CarouselServlet extends HttpServlet {
@EJB
private ProductFacade productFacade;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String path = request.getServletPath();
switch (path) {
case "/carousel":
List<Product> listProducts = productFacade.findAll();
request.setAttribute("listProducts", listProducts);
request.getRequestDispatcher(LoginServlet.pathToFile.getString("index")).forward(request, response);
break;
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<p class="w-100 text-center my-5" style="font-size: 24px">Добро пожаловать в ""!</p>
<form action="carousel">
<div class="ant-carousel">
<div class="ant-carousel-hider">
<ul class="ant-carousel-list">
<c:forEach var="product" items="${listProducts}">
<li class="ant-carousel-element">
<div class="card m-2">
<img src="insertCover/${product.cover.path}" class="card-img-top" alt="..."
style="max-width: 12rem; max-height: 15rem; margin: 0 auto">
<div class="card-body">
<h5 class="card-title">${product.title} ${product.model}</h5>
<p class="card-text">Цена: <strong>${product.price}€</strong></p>
<a href="#" class="btn btn-primary">Смотреть</a>
<a href="buyProductForm?productId=${product.id}" class="btn btn-primary">Купить</a>
</div>
</div>
</li>
</c:forEach>
</ul>
</div>
<div class="ant-carousel-arrow-left"></div>
<div class="ant-carousel-arrow-right"></div>
<div class="ant-carousel-dots"></div>
</div>
</form>
Так-же тут есть стили и должен быть Javascript кнопок, но думаю это можно самим найти
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question