Skip to content

Commit 146c1bb

Browse files
mmchsusanKevinGilmore
authored andcommitted
BAEL-1793 Spring with Thymeleaf Pagination for a List (eugenp#4290)
* BAEL-1793 Spring with Thymeleaf Pagination for a List * Replace tabs with 4 spaces in HTML based on editor's feedback.
1 parent 60cb1d7 commit 146c1bb

7 files changed

Lines changed: 239 additions & 11 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.thymeleaf.controller;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
7+
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.Model;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
14+
import com.baeldung.thymeleaf.model.Book;
15+
import com.baeldung.thymeleaf.model.Page;
16+
import com.baeldung.thymeleaf.utils.BookUtils;
17+
18+
@Controller
19+
public class BookController {
20+
21+
private static int currentPage = 1;
22+
private static int pageSize = 5;
23+
24+
@RequestMapping(value = "/listBooks", method = RequestMethod.GET)
25+
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
26+
page.ifPresent(p -> currentPage = p);
27+
size.ifPresent(s -> pageSize = s);
28+
29+
List<Book> books = BookUtils.buildBooks();
30+
Page<Book> bookPage = new Page<Book>(books, pageSize, currentPage);
31+
32+
model.addAttribute("books", bookPage.getList());
33+
model.addAttribute("selectedPage", bookPage.getCurrentPage());
34+
model.addAttribute("pageSize", pageSize);
35+
36+
int totalPages = bookPage.getTotalPages();
37+
model.addAttribute("totalPages", totalPages);
38+
39+
if (totalPages > 1) {
40+
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
41+
.boxed()
42+
.collect(Collectors.toList());
43+
model.addAttribute("pageNumbers", pageNumbers);
44+
}
45+
46+
return "listBooks.html";
47+
}
48+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.thymeleaf.model;
2+
3+
public class Book {
4+
private int id;
5+
private String name;
6+
7+
public Book(int id, String name) {
8+
super();
9+
this.id = id;
10+
this.name = name;
11+
}
12+
13+
public int getId() {
14+
return id;
15+
}
16+
17+
public void setId(int id) {
18+
this.id = id;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.thymeleaf.model;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
public class Page<T> {
7+
8+
private List<T> list;
9+
10+
private int pageSize = 0;
11+
12+
private int currentPage = 0;
13+
14+
private int totalPages = 0;
15+
16+
public Page(List<T> list, int pageSize, int currentPage) {
17+
18+
if (list.isEmpty()) {
19+
this.list = list;
20+
}
21+
22+
if (pageSize <= 0 || pageSize > list.size() || currentPage <= 0) {
23+
throw new IllegalArgumentException("invalid page size or current page!");
24+
}
25+
26+
this.pageSize = pageSize;
27+
28+
this.currentPage = currentPage;
29+
30+
if (list.size() % pageSize == 0) {
31+
this.totalPages = list.size() / pageSize;
32+
} else {
33+
this.totalPages = list.size() / pageSize + 1;
34+
}
35+
36+
int startItem = (currentPage - 1) * pageSize;
37+
if (list.size() < startItem) {
38+
this.list = Collections.emptyList();
39+
}
40+
41+
int toIndex = Math.min(startItem + pageSize, list.size());
42+
this.list = list.subList(startItem, toIndex);
43+
}
44+
45+
public List<T> getList() {
46+
return list;
47+
}
48+
49+
public int getPageSize() {
50+
return pageSize;
51+
}
52+
53+
public int getCurrentPage() {
54+
return currentPage;
55+
}
56+
57+
public int getTotalPages() {
58+
return totalPages;
59+
}
60+
61+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.thymeleaf.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.IntStream;
6+
7+
import com.baeldung.thymeleaf.model.Book;
8+
9+
public class BookUtils {
10+
11+
private static List<Book> books = new ArrayList<Book>();
12+
13+
private static final int NUM_BOOKS = 30;
14+
15+
private static final int MIN_BOOK_NUM = 1000;
16+
17+
public static List<Book> buildBooks() {
18+
if (books.isEmpty()) {
19+
IntStream.range(0, NUM_BOOKS).forEach(n -> {
20+
books.add(new Book(MIN_BOOK_NUM + n + 1, "Spring in Action"));
21+
});
22+
23+
}
24+
25+
return books;
26+
}
27+
28+
}
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
msg.id=ID
2-
msg.name=Name
3-
msg.gender=Gender
4-
msg.percent=Percentage
5-
welcome.message=Welcome Student !!!
6-
msg.AddStudent=Add Student
7-
msg.ListStudents=List Students
8-
msg.Home=Home
9-
msg.ListTeachers=List Teachers
10-
msg.courses=Courses
11-
msg.skills=Skills
1+
msg.id=ID
2+
msg.name=Name
3+
msg.gender=Gender
4+
msg.percent=Percentage
5+
welcome.message=Welcome Student !!!
6+
msg.AddStudent=Add Student
7+
msg.ListStudents=List Students
8+
msg.Home=Home
9+
msg.ListTeachers=List Teachers
10+
msg.ListBooks=List Books with paging
11+
msg.courses=Courses
12+
msg.skills=Skills
1213
msg.active=Active

‎spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ <h1>
2121
<tr>
2222
<td><a th:href="@{/listTeachers}" th:text="#{msg.ListTeachers}" /></td>
2323
</tr>
24+
<tr>
25+
<td><a th:href="@{/listBooks}" th:text="#{msg.ListBooks}" /></td>
26+
</tr>
2427
</table>
2528
</body>
2629
</html>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<style>
5+
.pagination {
6+
display: inline-block;
7+
}
8+
9+
.pagination a {
10+
color: black;
11+
float: left;
12+
padding: 5px 5px;
13+
text-decoration: none;
14+
}
15+
16+
.pagination a.active {
17+
background-color: gray;
18+
color: white;
19+
border-radius: 2px;
20+
}
21+
</style>
22+
<head>
23+
<title>Book List</title>
24+
</head>
25+
<body>
26+
<h1>Book List</h1>
27+
<table border="1">
28+
<thead>
29+
<tr>
30+
<th th:text="#{msg.id}" />
31+
<th th:text="#{msg.name}" />
32+
</tr>
33+
</thead>
34+
<tbody>
35+
<tr th:each="book, iStat : ${books}"
36+
th:style="${iStat.odd}? 'font-weight: bold;'"
37+
th:alt-title="${iStat.even}? 'even' : 'odd'">
38+
<td th:text="${book.id}" />
39+
<td th:text="${book.name}" />
40+
</tr>
41+
</tbody>
42+
</table>
43+
<div th:if="${totalPages > 1}" class="pagination"
44+
th:each="pageNumber : ${pageNumbers}">
45+
<a th:href="@{/listBooks(size=${pageSize}, page=${pageNumber})}"
46+
th:text=${pageNumber}
47+
th:class="${pageNumber==selectedPage} ? active"></a>
48+
</div>
49+
50+
<div>
51+
<p>
52+
<a th:href="@{/}" th:text="#{msg.Home}"></a>
53+
</p>
54+
</div>
55+
</body>
56+
</html>
57+
58+

0 commit comments

Comments
 (0)