Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.0 kB
6
Indexable
Never
 <ul class="pagination pagination-lg justify-content-end">
			    			<li class="page-item ${currentPage == 0 ? 'disabled' : ''}">
					        <a class="page-link rounded-0 mr-3 shadow-sm border-top-0 border-left-0 text-dark" href="/BanLaptop/home/shop.htm?page=${currentPage - 1}">Trước</a>
					    </li>
					    <c:if test="${currentPage > 1}">
					        <li class="page-item">
					            <a class="page-link rounded-0 mr-3 shadow-sm border-top-0 border-left-0 text-dark" href="/BanLaptop/home/shop.htm?page=0">1</a>
					        </li>
					        <li class="page-item disabled">
					            <a class="page-link rounded-0 mr-3 shadow-sm border-top-0 border-left-0 text-dark">...</a>
					        </li>
					    </c:if>
					    <c:forEach begin="${startPage}" end="${endPage}" var="i">
					        <li class="page-item ${currentPage == i ? 'active' : ''}">
					            <a class="${currentPage == i ? 'page-link active rounded-0 mr-3 shadow-sm border-top-0 border-left-0' : 'page-link rounded-0 mr-3 shadow-sm border-top-0 border-left-0 text-dark'}" href="/BanLaptop/home/shop.htm?page=${i}">${i + 1}</a>
					        </li>
					    </c:forEach>
					    <c:if test="${currentPage < totalPages - 2}">
					        <li class="page-item disabled">
					            <a class="page-link rounded-0 mr-3 shadow-sm border-top-0 border-left-0 text-dark">...</a>
					        </li>
					        <li class="page-item">
					            <a class="page-link rounded-0 mr-3 shadow-sm border-top-0 border-left-0 text-dark" href="/BanLaptop/home/shop.htm?page=${totalPages - 1}">${totalPages}</a>
					        </li>
					    </c:if>
					    <li class="page-item ${currentPage == totalPages - 1 ? 'disabled' : ''}">
					        <a class="page-link rounded-0 mr-3 shadow-sm border-top-0 border-left-0 text-dark" href="/BanLaptop/home/shop.htm?page=${currentPage + 1}">Sau</a>
					    </li>
					</ul>



@RequestMapping(value="shop", method = RequestMethod.GET)
	public String showShop(ModelMap model,
			@RequestParam(defaultValue = "0") int page
			) {
		int pageSize = 9;
		int totalLoaiSanPham = getSoLuongLoaiSanPham();
		int totalPages = (int) Math.ceil((double) totalLoaiSanPham / pageSize);
		int startPage = Math.max(0, page - 1);
		int endPage = Math.min(totalPages - 1, page + 1);
		
		List<LoaiSanPham> listLoaiSanPham = getLoaiSanPham(page,pageSize);
		System.out.println(listLoaiSanPham.size());
		model.addAttribute("currentPage", page);
		model.addAttribute("startPage", startPage);
		model.addAttribute("endPage", endPage);
		model.addAttribute("listLoaiSanPham", listLoaiSanPham);
		return "sp/shop";
	}



	private List<LoaiSanPham> getLoaiSanPham(int page, int pageSize) {
		Session session = factory.getCurrentSession();
		String hql = "FROM LoaiSanPham";
		Query query = session.createQuery(hql);
		int offset = page * pageSize;
		List<LoaiSanPham> list = query.setFirstResult(offset).setMaxResults(pageSize).list();
		return list;
	}