リストのためのThymeleafのページ付けを伴う春
1概要
このクイックチュートリアルでは、SpringとThymeleafを使用してページ区切り付きの項目のリストを表示する簡単なアプリケーションを作成します。
ThymeleafとSpringを統合する方法についての紹介は、私たちの記事リンク/thymeleaf-in-spring-mvc[こちら]を見てください。
2 Mavenの依存関係
通常のSpringの依存関係に加えて、ThymeleafとSpring Data Commonsの依存関係も追加します。
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.0.9.RELEASE</version>
</dependency>
最新のhttps://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22org.thymeleaf%22%20AND%20a%3A%22thymeleaf-spring5%22[thymeleaf-spring5]
https://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22org.springframework.data%22%20AND%20a%3A%22spring-data-commons%22
[spring-data- Maven Centralリポジトリ内の依存関係。
3.モデル
私たちのサンプルアプリケーションは本のリストのためのページ付けを示します。
まず、2つのフィールドとすべての引数を持つコンストラクタを持つ
Book
クラスを定義しましょう。
public class Book {
private int id;
private String name;
//standard constructor, setters and getters
}
4.サービス
次に、Spring Data Commonsライブラリを使用して、要求されたページのページ番号付きブックリストを生成するサービスを作成します。
@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;
}
}
上記のサービスでは、リクエストされたページに基づいて選択された
Page
を返すメソッドを作成しました。
PageImpl
クラスは、本のページ番号付きリストを除外するのに役立ちます。
5.スプリングコントローラー
ページサイズと現在のページ番号が与えられたときに、選択されたページのブックリストを取得するには、Springコントローラが必要です。
選択したページとページサイズにデフォルト値を使用するには、パラメータなしで
/listBooks
にあるリソースにアクセスするだけです。
ページサイズや特定のページが必要な場合は、
page
と
size
のパラメータを追加できます。
たとえば、__/listBooks?page = 2のようになります。
@Controller
public class BookController {
@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) {
int currentPage = page.orElse(1);
int pageSize = size.orElse(5);
Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
model.addAttribute("bookPage", bookPage);
int totalPages = bookPage.getTotalPages();
if (totalPages > 0) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed()
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
}
return "listBooks.html";
}
}
-
ビューのページ分割を準備するために、選択した
Page
とページ番号のリストを含む、モデル属性をSpringコントローラに追加しました** 。
6. Thymeleafテンプレート
これで、Thymeleafテンプレート
“ listBooks.html”
を作成する時が来ました。これは、
私たちのSpringコントローラーからのモデル属性に基づいてページ付けされた本のリスト
を表示します。
まず、本のリストを繰り返してテーブルに表示します。それから、総ページ数が0より大きい場合のページ区切りを表示します。
ページをクリックして選択するたびに、対応する書籍のリストが表示され、現在のページのリンクが強調表示されます。
<table border="1">
<thead>
<tr>
<th th:text="#{msg.id}"/>
<th th:text="#{msg.name}"/>
</tr>
</thead>
<tbody>
<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}"/>
<td th:text="${book.name}"/>
</tr>
</tbody>
</table>
<div th:if="${bookPage.totalPages > 0}" class="pagination"
th:each="pageNumber : ${pageNumbers}">
<a th:href="@{/listBooks(size=${bookPage.size}, page=${pageNumber})}"
th:text=${pageNumber}
th:class="${pageNumber==bookPage.number + 1} ? active"></a>
</div>
7.まとめ
この記事では、Thymeleafを使ってSpringフレームワークでリストをページ分割する方法を説明しました。
いつものように、この記事で使用されているすべてのコードサンプルはhttps://github.com/eugenp/tutorials/tree/master/spring-thymeleaf[GitHubで利用可能]です。