Untitled
unknown
plain_text
2 years ago
26 kB
10
Indexable
//Shop.jsp
<%--
Document : Shop
Created on : October 24, 2023, 2:10:09 PM
Author : Admin
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>-->
<meta charset="UTF-8">
<meta name="description" content="">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- The above 4 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<!-- Title -->
<title>KiA Shop - Be good, Be bad, Be yourself | Shop</title>
<!-- Favicon -->
<link rel="icon" href="img/core-img/favicon2.ico">
<!-- Core Style CSS -->
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<link rel="stylesheet" href="css/core-style.css">
<link rel="stylesheet" href="style.css">
<style>
.searchbar{
margin-bottom: auto;
margin-top: auto;
height: 50px;
background-color: #f5f7fa;
border-radius: 30px;
padding: 8px;
}
.search_input{
color: black;
border: 0;
outline: 0;
background: none;
width: 0;
caret-color:transparent;
line-height: 40px;
transition: width 0.4s linear;
}
.searchbar > .search_input{
padding: 0 10px;
width: 360px;
caret-color:red;
transition: width 0.4s linear;
}
.searchbar > .search_icon{
background: #f5f7fa;
color: #e74c3c;
}
.search_icon{
height: 40px;
width: 40px;
float: right;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
color:white;
text-decoration:none;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<!-- Search Wrapper Area Start -->
<jsp:include page="common/search.jsp"></jsp:include>
<!-- Search Wrapper Area End -->
<!-- ##### Main Content Wrapper Start ##### -->
<jsp:include page="common/header.jsp"></jsp:include>
<!-- Header Area End -->
<div class="shop_sidebar_area">
<div class="product-topbar d-xl-flex align-items-end justify-content-between">
<!-- Total Products -->
<div class="total-products">
<p>View All Products</p>
<div class="view d-flex">
<a href="ShopControl?showAllProducts=true"><i class="fa fa-th-large" aria-hidden="true"></i></a>
</div>
</div></div>
<!-- ##### Single Widget ##### -->
<div class="widget catagory mb-50">
<!-- Widget Title -->
<h6 class="widget-title mb-30">Catagories</h6>
<!-- Catagories -->
<div class="catagories-menu">
<ul id="categoryMenu">
<c:forEach items="${listC}" var="o">
<li class="${tag == o.id ? "active":""}">
<a href="ShopControl?pageIndex=1&cID=${o.id}">${o.name}</a>
</li>
</c:forEach>
</ul>
</div>
</div>
<!-- ##### Single Widget ##### -->
<div class="widget brands mb-50">
<!-- Widget Title -->
<h6 class="widget-title mb-30">Price</h6>
<div class="widget-desc">
<!-- Single Form Check -->
<div class="form-check">
<input class="form-check-input" type="radio" value="1" id="amado" name="price">
<label class="form-check-label" for="amado">Under $100</label>
</div>
<!-- Single Form Check -->
<div class="form-check">
<input class="form-check-input" type="radio" value="2" id="ikea" name="price">
<label class="form-check-label" for="ikea">$100 - $300</label>
</div>
<!-- Single Form Check -->
<div class="form-check">
<input class="form-check-input" type="radio" value="3" id="furniture" name="price">
<label class="form-check-label" for="furniture">$300 - $500</label>
</div>
<!-- Single Form Check -->
<div class="form-check">
<input class="form-check-input" type="radio" value="4" id="factory" name="price">
<label class="form-check-label" for="factory">$500 - $1000</label>
</div>
<!-- Single Form Check -->
<div class="form-check">
<input class="form-check-input" type="radio" value="5" id="artdeco" name="price">
<label class="form-check-label" for="artdeco">$1000 +</label>
</div>
</div>
</div>
<!-- ##### Single Widget ##### -->
<div class="widget color mb-50">
<!-- Widget Title
<h6 class="widget-title mb-30">Color</h6>
<div class="widget-desc">
<ul class="d-flex">
<li>
<a href="#" class="color1"></a>
</li>
<li>
<a href="#" class="color2"></a>
</li>
<li>
<a href="#" class="color3"></a>
</li>
<li>
<a href="#" class="color4"></a>
</li>
<li>
<a href="#" class="color5"></a>
</li>
<li>
<a href="#" class="color6"></a>
</li>
<li>
<a href="#" class="color7"></a>
</li>
<li>
<a href="#" class="color8"></a>
</li>
</ul>
</div>-->
</div>
</div>
<div class="amado_product_area section-padding-100">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="product-topbar d-xl-flex align-items-end justify-content-between">
<!-- Search Form -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<form action="SearchControl" method="get">
<input name="txt" type="text" placeholder="Search by name" value="${txtSearch}">
<select name="cID" ${showAllProducts ? 'disabled' : ''}>
<c:if test="${empty cID || showAllProducts}">
<option ${empty cID && showAllProducts ? 'selected' : ''}>All Products</option>
</c:if>
<c:forEach var="category" items="${listC}">
<c:if test="${!empty cID && category.id eq cID}">
<option value="${category.id}"selected>${category.name}</option>
</c:if>
</c:forEach>
</select>
<button type="submit">Search</button>
</form>
<!-- Sorting -->
<div class="product-sorting d-flex">
<div class="sort-by-date d-flex align-items-center mr-15">
<p>Sort</p>
<form action="#" method="get">
<select name="select" id="sortBydate">
<option value="value">Oldest</option>
<option value="value">Newest</option>
<option value="value">Popular</option>
<option value="value">Price</option>
</select>
</form>
</div>
<div class="view-product d-flex align-items-center">
<p>View</p>
<form action="ShopControl" method="post">
<select name="viewPage" id="viewProduct" onchange="this.form.submit()">
<c:forEach items="${listView}" var="o">
<option ${numberProduct == o?"selected":""} value="${o}">${o}</option>
</c:forEach>
</select>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- Đoạn này để hiển thị danh sách sản phẩm -->
<div id="content" class="row">
<c:forEach items="${listP}" var="o">
<!-- Single Product Area -->
<div class="col-12 col-sm-6 col-md-12 col-xl-6">
<div class="single-product-wrapper">
<!-- Product Image -->
<div class="product-img">
<img src="${o.image}" alt="">
<!-- Hover Thumb -->
<!--<img class="hover-img" src="img/product-img/product5.jpg" alt="">-->
</div>
<!-- Product Description -->
<div class="product-description d-flex align-items-center justify-content-between">
<!-- Product Meta Data -->
<div class="product-meta-data">
<div class="line"></div>
<p class="product-price">$${o.price}</p>
<a href="productDetail?productID=${o.id}">
<h6>${o.name}</h6>
</a>
</div>
<!-- Ratings & Cart -->
<div class="ratings-cart text-right">
<div class="ratings">
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
<i class="fa fa-star" aria-hidden="true"></i>
</div>
<div class="cart">
<c:if test="${o.amount != 0}">
<a href="cart?id=${o.id}&action=add" data-toggle="tooltip" data-placement="left" title="Add to Cart">
<img src="img/core-img/cart.png" alt="">
</a>
<a href="Shop.jsp" style='font-size:16px;' title="Add to favourite">🧡</a>
</c:if>
<c:if test="${o.amount == 0}">
<a href="ShopControl" data-toggle="tooltip" data-placement="left">Sold out</a>
<a href="FavouriteControl" style='font-size:16px;' title="Add to favourite">🧡</a>
</c:if>
</div>
</div>
</div>
</div>
</div>
</c:forEach>
</div>
<div class="row">
<div class="col-12">
<!-- Pagination -->
<nav aria-label="navigation">
<ul class="pagination justify-content-end mt-50" id="paging">
<c:forEach begin="1" end="${totalPage}" var="i">
<c:url var="pageURL" value="ShopControl">
<c:param name="pageIndex" value="${i}" />
<!-- Kiểm tra showAllProducts để thêm tham số cID khi cần -->
<c:if test="${!showAllProducts}">
<c:param name="cID" value="${cID}" />
</c:if>
<c:if test="${showAllProducts==true}">
<c:param name="showAllProducts" value="${showAllProducts}" />
</c:if>
</c:url>
<li class="page-item ${pageIndex == i ? 'active' : ''}">
<a class="page-link" href="${pageURL}">
${i}
</a>
</li>
</c:forEach>
</ul>
</nav>
</div>
</div>
</div>
</div>
<input type="hidden" id="categoryID" value="${cID}"/>
</div>
<!-- ##### Main Content Wrapper End ##### -->
<!-- ##### Newsletter Area Start ##### -->
<!-- ##### Newsletter Area End ##### -->
<!-- ##### Footer Area Start ##### -->
<jsp:include page="common/footer.jsp"></jsp:include>
<!-- ##### Footer Area End ##### -->
<!-- ##### jQuery (Necessary for All JavaScript Plugins) ##### -->
<script src="js/jquery/jquery-2.2.4.min.js"></script>
<!-- Popper js -->
<script src="js/popper.min.js"></script>
<!-- Bootstrap js -->
<script src="js/bootstrap.min.js"></script>
<!-- Plugins js -->
<script src="js/plugins.js"></script>
<!-- Active js -->
<script src="js/active.js"></script>
<!-- Thêm script để thực hiện chức năng tìm kiếm -->
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</body>
</html>
//ShopControl.java
//@WebServlet(name = "ShopControl", urlPatterns = {"/ShopControl"})
public class ShopControl extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DAO dao = new DAO();
try {
List<Category> listC = dao.getAllCategory();
request.setAttribute("listC", listC);
String currentCID = request.getParameter("cID");
String showAllProductsParam = request.getParameter("showAllProducts");
currentCID = (currentCID == null || currentCID.equals("")) ? String.valueOf(listC.get(0).getId()) : currentCID;
boolean showAllProducts = Boolean.parseBoolean(showAllProductsParam);
if (!showAllProducts) {
request.setAttribute("cID", currentCID);
}
request.setAttribute("showAllProducts", showAllProducts);
int pageIndex = (request.getParameter("pageIndex") != null) ? Integer.parseInt(request.getParameter("pageIndex")) : 1;
if (showAllProducts) {
List<Product> allProducts = dao.getAllProduct();
int numberProductPerPage = 10;
int pageSize = getPageSize(numberProductPerPage, allProducts.size());
List<Product> paginatedProducts = paginateList(allProducts, pageIndex, numberProductPerPage);
request.setAttribute("listP", paginatedProducts);
request.setAttribute("pageIndex", pageIndex);
request.setAttribute("totalPage", pageSize);
} else {
int numberProduct = (request.getParameter("numberProduct") != null) ? Integer.parseInt(request.getParameter("numberProduct")) : 6;
List<Product> ls = dao.getProductByCid(currentCID);
int pageSize = getPageSize(numberProduct, dao.getProductByCid(currentCID).size());
request.setAttribute("listP", ls);
request.setAttribute("numberProduct", numberProduct);
request.setAttribute("pageIndex", pageIndex);
request.setAttribute("totalPage", pageSize);
request.setAttribute("tag", currentCID);
}
request.getRequestDispatcher("Shop.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
request.getRequestDispatcher("Shop.jsp").forward(request, response);
}
}
// Hàm phân trang danh sách sản phẩm
public List<Product> paginateList(List<Product> productList, int pageIndex, int numberProductPerPage) {
int start = (pageIndex - 1) * numberProductPerPage;
int end = Math.min(start + numberProductPerPage, productList.size());
return productList.subList(start, end);
}
public int getPageSize(int numberProduct, int allProduct) {
int pageSize = allProduct / numberProduct;
if (allProduct % numberProduct != 0) {
pageSize = (allProduct / numberProduct) + 1;
}
return pageSize;
}
//SearchControl
@WebServlet(name = "SearchControl", urlPatterns = {"/SearchControl"})
public class SearchControl extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String txtSearch = request.getParameter("txt");
String cID = request.getParameter("cID");
DAO dao = new DAO();
List<Category> listC = dao.getAllCategory();
request.setAttribute("listC", listC);
List<Product> list;
if (cID != null && !cID.isEmpty()) {
list = dao.search(txtSearch, cID);
} else {
// Nếu không có danh mục, chỉ thực hiện tìm kiếm theo tên
list = dao.searchByName(txtSearch);
}
request.setAttribute("listP", list);
request.setAttribute("txtSearch", txtSearch);
request.setAttribute("cID", cID);
request.getRequestDispatcher("Shop.jsp").forward(request, response);
}
//DAO.java
public List<Product> search(String txtSearch, String cID) {
List<Product> list = new ArrayList<>();
String query = "SELECT * FROM Product WHERE isDeleted != 1";
if (cID != null && !cID.trim().equals("")) {
query += " AND cID = ?";
}
if (txtSearch != null && !txtSearch.trim().equals("")) {
query += " AND pName LIKE ?";
}
try {
DAO dao = new DAO();
conn = new DBContext().getConnection();
ps = conn.prepareStatement(query);
int paramIndex = 1;
if (cID != null && !cID.trim().equals("")) {
ps.setInt(paramIndex++, Integer.parseInt(cID));
}
if (txtSearch != null && !txtSearch.trim().equals("")) {
ps.setString(paramIndex++, "%" + txtSearch + "%");
}
rs = ps.executeQuery();
while (rs.next()) {
list.add(new Product(rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getDouble(4),
rs.getString(5),
rs.getString(6),
rs.getInt(7), dao.getAllSubImageByPID(rs.getInt(1) + ""), rs.getInt(8), rs.getInt(9)));
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public List<Product> searchWithPaging(String txtSearch, int pageIndex, int pageSize, String cID) {
List<Product> list = new ArrayList<>();
String query = "SELECT * FROM Product WHERE isDeleted != 1";
if (cID != null && !cID.trim().equals("")) {
query += " AND cID = ?";
}
if (txtSearch != null && !txtSearch.trim().equals("")) {
query += " AND pName LIKE ?";
}
query += " ORDER BY pID OFFSET (? * ? - ?) ROWS FETCH NEXT ? ROWS ONLY";
try {
DAO dao = new DAO();
conn = new DBContext().getConnection();
ps = conn.prepareStatement(query);
int paramIndex = 1;
if (cID != null && !cID.trim().equals("")) {
ps.setInt(paramIndex++, Integer.parseInt(cID));
}
if (txtSearch != null && !txtSearch.trim().equals("")) {
ps.setString(paramIndex++, "%" + txtSearch + "%");
}
ps.setInt(paramIndex++, pageIndex);
ps.setInt(paramIndex++, pageSize);
ps.setInt(paramIndex++, pageSize);
ps.setInt(paramIndex, pageSize);
rs = ps.executeQuery();
while (rs.next()) {
list.add(new Product(rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getDouble(4),
rs.getString(5),
rs.getString(6),
rs.getInt(7), dao.getAllSubImageByPID(rs.getInt(1) + ""), rs.getInt(8), rs.getInt(9)));
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//Sua code
public List<Product> searchByName(String txtSearch) {
List<Product> list = new ArrayList<>();
String query = "select * from Product where pName like ?";
try {
conn = new DBContext().getConnection(); //mo ket noi toi sql
ps = conn.prepareStatement(query);//nem cau lenh query sang sql
ps.setString(1, "%" + txtSearch + "%");
rs = ps.executeQuery();//chay cau lenh query, nhan ket qua tra ve
DAO dao = new DAO();
while (rs.next()) {
list.add(new Product(rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getDouble(4),
rs.getString(5),
rs.getString(6),
rs.getInt(7), dao.getAllSubImageByPID(rs.getInt(1) + ""), rs.getInt(8), rs.getInt(9)));
}
} catch (Exception e) {
}
return list;
}
Editor is loading...
Leave a Comment