#171 - Switched to Thymeleaf Spring Data dialect for pagination in views.

Added dependency and bean definition for the SpringDataDialect. Templates now use three different elements of the Thymeleaf Spring Data support:

- Pagination information (pagination summary)
- The pagination links including first / previous and next / last links
- Pagination links to navigate through the pages
This commit is contained in:
Oliver Gierke
2016-03-20 11:09:17 +01:00
parent 91c06febe4
commit 75d25de1e6
3 changed files with 32 additions and 24 deletions

View File

@@ -43,6 +43,12 @@
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>io.github.jpenren</groupId>
<artifactId>thymeleaf-spring-data-dialect</artifactId>
<version>2.1.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.webjars</groupId> <groupId>org.webjars</groupId>
<artifactId>jquery</artifactId> <artifactId>jquery</artifactId>

View File

@@ -20,8 +20,10 @@ import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.dialect.springdata.SpringDataDialect;
/** /**
* @author Christoph Strobl * @author Christoph Strobl
@@ -44,6 +46,11 @@ public class Application extends WebMvcConfigurerAdapter {
resourceChain(true); resourceChain(true);
} }
@Bean
SpringDataDialect springDataDialect() {
return new SpringDataDialect();
}
@Autowired UserRepository repo; @Autowired UserRepository repo;
@PostConstruct @PostConstruct

View File

@@ -1,26 +1,26 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sd="http://www.thymeleaf.org/spring-data">
<head> <head>
<title>User Center</title> <title>User Center</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" /> <link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" />
</head> </head>
<body> <body>
<div class="container-fluid"> <div class="container-fluid">
<h1>User Center</h1> <h1>User Center</h1>
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">
<h3 class="panel-title">Search</h3> <h3 class="panel-title">Search</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<form action="/"> <form action="/">
<div class="form-group col-sm-6"> <div class="form-group col-sm-6">
<label for="firstname" class="control-label">Firstname:</label> <label for="firstname" class="control-label">Firstname:</label>
<input id="firstname" name="firstname" th:value="${#httpServletRequest.getParameter('firstname')}" type="text" class="form-control" autofocus="autofocus" /> <input id="firstname" name="firstname" th:value="${#httpServletRequest.getParameter('firstname')}" type="text" class="form-control" autofocus="autofocus" />
@@ -44,33 +44,26 @@
<div class="form-group col-sm-12"> <div class="form-group col-sm-12">
<button id="submit" type="submit" class="btn btn-default">Submit</button> <button id="submit" type="submit" class="btn btn-default">Submit</button>
</div> </div>
</form> </form>
</div> </div>
</div> </div>
<nav class="text-center"> <nav class="text-center">
<ul class="pagination" th:with="total = ${users.totalPages}"> <div sd:pagination-summary="true">Pagination information</div>
<li th:if="${users.hasPrevious()}"> <ul class="pagination" sd:pagination="full">
<a th:href="@{${baseUri}(page=${users.previousPageable().pageNumber})}" aria-label="Previous"> <!-- Pagination created by the Thymeleaf Spring Data dialect, this content is just for mockup -->
<span aria-hidden="true">&laquo;</span> <li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
</a> <li class="active"><a href="#">1 <span class="sr-only">(current)</span></a></li>
</li>
<li th:each="page : ${#numbers.sequence(0, total - 1)}"><a th:href="@{${baseUri}(page=${page})}" th:text="${page + 1}">1</a></li>
<li th:if="${users.hasNext()}">
<a th:href="@{${baseUri}(page=${users.nextPageable().pageNumber})}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul> </ul>
</nav> </nav>
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
<th>#</th> <th>#</th>
<th></th> <th></th>
<th>Firstname</th> <th><a sd:pagination-sort="firstname">Firstname</a></th>
<th>Lastname</th> <th>Lastname</th>
<th>Nationality</th> <th>Nationality</th>
<th>City</th> <th>City</th>
@@ -93,8 +86,10 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<ul class="pagination" sd:pagination="aligned-links"></ul>
</div> </div>
</body> </body>
</html> </html>