Skip to content

Commit 77d5c63

Browse files
feat(user): cursor pagnination
1 parent d1e54c0 commit 77d5c63

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/java/com/library/controller/book/BookListController.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@ public BookListController() {
4141
protected void doGet(HttpServletRequest request, HttpServletResponse response)
4242
throws ServletException, IOException {
4343

44+
String cursorParam = request.getParameter("cursor");
45+
String limitParam = request.getParameter("limit");
46+
47+
int cursor = (cursorParam == null) ? 0 : Integer.parseInt(cursorParam);
48+
int limit = (limitParam == null) ? 20 : Integer.parseInt(limitParam);
49+
4450
try {
45-
List<Book> bookList = bookDao.getAllBook();
51+
52+
List<Book> bookList = bookDao.getBooksByCursor(cursor, limit);
53+
54+
int nextCursor = bookList.isEmpty() ? 0 : bookList.get(bookList.size() - 1).getBookID();
55+
4656
request.setAttribute("bookList", bookList);
57+
request.setAttribute("nextCursor", nextCursor);
58+
request.setAttribute("limit", limit);
59+
4760
request.getRequestDispatcher("/WEB-INF/views/book/booklist.jsp").forward(request, response);
61+
4862
} catch (BookDataAccessException b) {
4963
logger.error("Error loading books", b);
5064
}

src/java/com/library/dao/BookDaoImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ public List<Book> getBooksByCursor(int cursor, int limit) {
369369

370370
try (Connection conn = DBConnection.getInstance().getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
371371

372-
ps.setInt(1, cursor); // ID cuối của trang trước (0 = trang đầu)
373-
ps.setInt(2, limit); // số bản ghi muốn lấy
372+
ps.setInt(1, cursor);
373+
ps.setInt(2, limit);
374374

375375
try (ResultSet rs = ps.executeQuery()) {
376376
while (rs.next()) {

web/WEB-INF/views/book/booklist.jsp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@
321321
<i class="fa-solid fa-heart"></i> Favorite
322322
</a>
323323
<a href="${pageContext.request.contextPath}/user/setting" class="dropdown-item">
324-
<i class="fa-solid fa-gear"></i> Setting
324+
<i class="fa-solid fa-gear"></i> Setting
325325
</a>
326326
<a href="${pageContext.request.contextPath}/LogOut" class="dropdown-item logout">
327327
<i class="fa-solid fa-right-from-bracket"></i> Logout
@@ -347,6 +347,16 @@
347347
</c:forEach>
348348
</div>
349349
</div>
350+
<!-- Pagination Cursor -->
351+
<div class="pagination" style="margin: 30px auto; text-align: center;">
352+
<c:if test="${nextCursor > 0}">
353+
<a href="${pageContext.request.contextPath}/book/list?cursor=${nextCursor}&limit=${limit}"
354+
style="padding: 12px 20px; background: #4f46e5; color: white;
355+
border-radius: 8px; text-decoration:none; font-weight:600;">
356+
Load More <i class="fa-solid fa-chevron-down"></i>
357+
</a>
358+
</c:if>
359+
</div>
350360

351361
<!-- ======= FOOTER ======= -->
352362
<footer class="footer">

0 commit comments

Comments
 (0)