Skip to content

Commit 9c8d31a

Browse files
mmchsusanKevinGilmore
authored andcommitted
BAEL 1793 Adding PageImpl for pagination (eugenp#4371)
* BAEL-1793 Spring with Thymeleaf Pagination for a List * Replace tabs with 4 spaces in HTML based on editor's feedback. * Updated to use spring data PageImpl for representing paged list
1 parent b1b34e2 commit 9c8d31a

4 files changed

Lines changed: 62 additions & 15 deletions

File tree

‎spring-thymeleaf/pom.xml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@
9898
<version>${springframework-security.version}</version>
9999
<scope>test</scope>
100100
</dependency>
101+
102+
<!-- Spring data -->
103+
<dependency>
104+
<groupId>org.springframework.data</groupId>
105+
<artifactId>spring-data-commons</artifactId>
106+
<version>${springFramework-data.version}</version>
107+
</dependency>
101108
</dependencies>
102109

103110
<build>
@@ -117,7 +124,7 @@
117124
<configuration>
118125
<wait>true</wait>
119126
<container>
120-
<containerId>jetty8x</containerId>
127+
<containerId>jetty9x</containerId>
121128
<type>embedded</type>
122129
<systemProperties>
123130
</systemProperties>
@@ -157,6 +164,7 @@
157164
<!-- spring -->
158165
<org.springframework-version>4.3.4.RELEASE</org.springframework-version>
159166
<springframework-security.version>4.2.0.RELEASE</springframework-security.version>
167+
<springFramework-data.version>2.0.7.RELEASE</springFramework-data.version>
160168
<javax.servlet-version>3.1.0</javax.servlet-version>
161169
<!-- thymeleaf -->
162170
<org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version>

‎spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/BookController.java‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,39 @@
55
import java.util.stream.Collectors;
66
import java.util.stream.IntStream;
77

8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.domain.Page;
10+
import org.springframework.data.domain.PageRequest;
11+
812
import org.springframework.stereotype.Controller;
913
import org.springframework.ui.Model;
1014
import org.springframework.web.bind.annotation.RequestMapping;
1115
import org.springframework.web.bind.annotation.RequestMethod;
1216
import org.springframework.web.bind.annotation.RequestParam;
1317

1418
import com.baeldung.thymeleaf.model.Book;
15-
import com.baeldung.thymeleaf.model.Page;
16-
import com.baeldung.thymeleaf.utils.BookUtils;
19+
import com.baeldung.thymeleaf.service.BookService;
1720

1821
@Controller
1922
public class BookController {
2023

2124
private static int currentPage = 1;
2225
private static int pageSize = 5;
2326

27+
@Autowired
28+
private BookService bookService;
29+
2430
@RequestMapping(value = "/listBooks", method = RequestMethod.GET)
2531
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
2632
page.ifPresent(p -> currentPage = p);
2733
size.ifPresent(s -> pageSize = s);
2834

29-
List<Book> books = BookUtils.buildBooks();
30-
Page<Book> bookPage = new Page<Book>(books, pageSize, currentPage);
35+
Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
3136

32-
model.addAttribute("books", bookPage.getList());
33-
model.addAttribute("selectedPage", bookPage.getCurrentPage());
34-
model.addAttribute("pageSize", pageSize);
37+
model.addAttribute("bookPage", bookPage);
3538

3639
int totalPages = bookPage.getTotalPages();
37-
model.addAttribute("totalPages", totalPages);
38-
39-
if (totalPages > 1) {
40+
if (totalPages > 0) {
4041
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
4142
.boxed()
4243
.collect(Collectors.toList());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.thymeleaf.service;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.data.domain.PageImpl;
8+
import org.springframework.data.domain.PageRequest;
9+
import org.springframework.data.domain.Pageable;
10+
import org.springframework.stereotype.Service;
11+
12+
import com.baeldung.thymeleaf.model.Book;
13+
import com.baeldung.thymeleaf.utils.BookUtils;
14+
15+
@Service
16+
public class BookService {
17+
18+
final private List<Book> books = BookUtils.buildBooks();
19+
20+
public Page<Book> findPaginated(Pageable pageable) {
21+
int pageSize = pageable.getPageSize();
22+
int currentPage = pageable.getPageNumber();
23+
int startItem = currentPage * pageSize;
24+
List<Book> list;
25+
26+
if (books.size() < startItem) {
27+
list = Collections.emptyList();
28+
} else {
29+
int toIndex = Math.min(startItem + pageSize, books.size());
30+
list = books.subList(startItem, toIndex);
31+
}
32+
33+
Page<Book> bookPage = new PageImpl<Book>(list, PageRequest.of(currentPage, pageSize), books.size());
34+
35+
return bookPage;
36+
37+
}
38+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ <h1>Book List</h1>
3232
</tr>
3333
</thead>
3434
<tbody>
35-
<tr th:each="book, iStat : ${books}"
35+
<tr th:each="book, iStat : ${bookPage.content}"
3636
th:style="${iStat.odd}? 'font-weight: bold;'"
3737
th:alt-title="${iStat.even}? 'even' : 'odd'">
3838
<td th:text="${book.id}" />
3939
<td th:text="${book.name}" />
4040
</tr>
4141
</tbody>
4242
</table>
43-
<div th:if="${totalPages > 1}" class="pagination"
43+
<div th:if="${bookPage.totalPages > 0}" class="pagination"
4444
th:each="pageNumber : ${pageNumbers}">
45-
<a th:href="@{/listBooks(size=${pageSize}, page=${pageNumber})}"
45+
<a th:href="@{/listBooks(size=${bookPage.size}, page=${pageNumber})}"
4646
th:text=${pageNumber}
47-
th:class="${pageNumber==selectedPage} ? active"></a>
47+
th:class="${pageNumber==bookPage.number + 1} ? active"></a>
4848
</div>
4949

5050
<div>

0 commit comments

Comments
 (0)