BAEL 1793 Adding PageImpl for pagination (#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
This commit is contained in:
parent
b1b34e2fca
commit
9c8d31aae6
@ -98,6 +98,13 @@
|
||||
<version>${springframework-security.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring data -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
<version>${springFramework-data.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -117,7 +124,7 @@
|
||||
<configuration>
|
||||
<wait>true</wait>
|
||||
<container>
|
||||
<containerId>jetty8x</containerId>
|
||||
<containerId>jetty9x</containerId>
|
||||
<type>embedded</type>
|
||||
<systemProperties>
|
||||
</systemProperties>
|
||||
@ -157,6 +164,7 @@
|
||||
<!-- spring -->
|
||||
<org.springframework-version>4.3.4.RELEASE</org.springframework-version>
|
||||
<springframework-security.version>4.2.0.RELEASE</springframework-security.version>
|
||||
<springFramework-data.version>2.0.7.RELEASE</springFramework-data.version>
|
||||
<javax.servlet-version>3.1.0</javax.servlet-version>
|
||||
<!-- thymeleaf -->
|
||||
<org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version>
|
||||
|
@ -5,6 +5,10 @@ import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -12,8 +16,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Book;
|
||||
import com.baeldung.thymeleaf.model.Page;
|
||||
import com.baeldung.thymeleaf.utils.BookUtils;
|
||||
import com.baeldung.thymeleaf.service.BookService;
|
||||
|
||||
@Controller
|
||||
public class BookController {
|
||||
@ -21,22 +24,20 @@ public class BookController {
|
||||
private static int currentPage = 1;
|
||||
private static int pageSize = 5;
|
||||
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
|
||||
@RequestMapping(value = "/listBooks", method = RequestMethod.GET)
|
||||
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
|
||||
page.ifPresent(p -> currentPage = p);
|
||||
size.ifPresent(s -> pageSize = s);
|
||||
|
||||
List<Book> books = BookUtils.buildBooks();
|
||||
Page<Book> bookPage = new Page<Book>(books, pageSize, currentPage);
|
||||
Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
|
||||
|
||||
model.addAttribute("books", bookPage.getList());
|
||||
model.addAttribute("selectedPage", bookPage.getCurrentPage());
|
||||
model.addAttribute("pageSize", pageSize);
|
||||
model.addAttribute("bookPage", bookPage);
|
||||
|
||||
int totalPages = bookPage.getTotalPages();
|
||||
model.addAttribute("totalPages", totalPages);
|
||||
|
||||
if (totalPages > 1) {
|
||||
if (totalPages > 0) {
|
||||
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.thymeleaf.service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Book;
|
||||
import com.baeldung.thymeleaf.utils.BookUtils;
|
||||
|
||||
@Service
|
||||
public class BookService {
|
||||
|
||||
final private List<Book> books = BookUtils.buildBooks();
|
||||
|
||||
public Page<Book> findPaginated(Pageable pageable) {
|
||||
int pageSize = pageable.getPageSize();
|
||||
int currentPage = pageable.getPageNumber();
|
||||
int startItem = currentPage * pageSize;
|
||||
List<Book> list;
|
||||
|
||||
if (books.size() < startItem) {
|
||||
list = Collections.emptyList();
|
||||
} else {
|
||||
int toIndex = Math.min(startItem + pageSize, books.size());
|
||||
list = books.subList(startItem, toIndex);
|
||||
}
|
||||
|
||||
Page<Book> bookPage = new PageImpl<Book>(list, PageRequest.of(currentPage, pageSize), books.size());
|
||||
|
||||
return bookPage;
|
||||
|
||||
}
|
||||
}
|
@ -32,7 +32,7 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="book, iStat : ${books}"
|
||||
<tr th:each="book, iStat : ${bookPage.content}"
|
||||
th:style="${iStat.odd}? 'font-weight: bold;'"
|
||||
th:alt-title="${iStat.even}? 'even' : 'odd'">
|
||||
<td th:text="${book.id}" />
|
||||
@ -40,11 +40,11 @@
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div th:if="${totalPages > 1}" class="pagination"
|
||||
<div th:if="${bookPage.totalPages > 0}" class="pagination"
|
||||
th:each="pageNumber : ${pageNumbers}">
|
||||
<a th:href="@{/listBooks(size=${pageSize}, page=${pageNumber})}"
|
||||
<a th:href="@{/listBooks(size=${bookPage.size}, page=${pageNumber})}"
|
||||
th:text=${pageNumber}
|
||||
th:class="${pageNumber==selectedPage} ? active"></a>
|
||||
th:class="${pageNumber==bookPage.number + 1} ? active"></a>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user