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:
mmchsusan 2018-06-26 23:00:40 -04:00 committed by KevinGilmore
parent b1b34e2fca
commit 9c8d31aae6
4 changed files with 62 additions and 15 deletions

View File

@ -98,6 +98,13 @@
<version>${springframework-security.version}</version> <version>${springframework-security.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- Spring data -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${springFramework-data.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -117,7 +124,7 @@
<configuration> <configuration>
<wait>true</wait> <wait>true</wait>
<container> <container>
<containerId>jetty8x</containerId> <containerId>jetty9x</containerId>
<type>embedded</type> <type>embedded</type>
<systemProperties> <systemProperties>
</systemProperties> </systemProperties>
@ -157,6 +164,7 @@
<!-- spring --> <!-- spring -->
<org.springframework-version>4.3.4.RELEASE</org.springframework-version> <org.springframework-version>4.3.4.RELEASE</org.springframework-version>
<springframework-security.version>4.2.0.RELEASE</springframework-security.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> <javax.servlet-version>3.1.0</javax.servlet-version>
<!-- thymeleaf --> <!-- thymeleaf -->
<org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version> <org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version>

View File

@ -5,6 +5,10 @@ import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; 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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; 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 org.springframework.web.bind.annotation.RequestParam;
import com.baeldung.thymeleaf.model.Book; import com.baeldung.thymeleaf.model.Book;
import com.baeldung.thymeleaf.model.Page; import com.baeldung.thymeleaf.service.BookService;
import com.baeldung.thymeleaf.utils.BookUtils;
@Controller @Controller
public class BookController { public class BookController {
@ -21,22 +24,20 @@ public class BookController {
private static int currentPage = 1; private static int currentPage = 1;
private static int pageSize = 5; private static int pageSize = 5;
@Autowired
private BookService bookService;
@RequestMapping(value = "/listBooks", method = RequestMethod.GET) @RequestMapping(value = "/listBooks", method = RequestMethod.GET)
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) { public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
page.ifPresent(p -> currentPage = p); page.ifPresent(p -> currentPage = p);
size.ifPresent(s -> pageSize = s); size.ifPresent(s -> pageSize = s);
List<Book> books = BookUtils.buildBooks(); Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
Page<Book> bookPage = new Page<Book>(books, pageSize, currentPage);
model.addAttribute("books", bookPage.getList()); model.addAttribute("bookPage", bookPage);
model.addAttribute("selectedPage", bookPage.getCurrentPage());
model.addAttribute("pageSize", pageSize);
int totalPages = bookPage.getTotalPages(); int totalPages = bookPage.getTotalPages();
model.addAttribute("totalPages", totalPages); if (totalPages > 0) {
if (totalPages > 1) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages) List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed() .boxed()
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -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;
}
}

View File

@ -32,7 +32,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr th:each="book, iStat : ${books}" <tr th:each="book, iStat : ${bookPage.content}"
th:style="${iStat.odd}? 'font-weight: bold;'" th:style="${iStat.odd}? 'font-weight: bold;'"
th:alt-title="${iStat.even}? 'even' : 'odd'"> th:alt-title="${iStat.even}? 'even' : 'odd'">
<td th:text="${book.id}" /> <td th:text="${book.id}" />
@ -40,11 +40,11 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<div th:if="${totalPages > 1}" class="pagination" <div th:if="${bookPage.totalPages > 0}" class="pagination"
th:each="pageNumber : ${pageNumbers}"> 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:text=${pageNumber}
th:class="${pageNumber==selectedPage} ? active"></a> th:class="${pageNumber==bookPage.number + 1} ? active"></a>
</div> </div>
<div> <div>