如何在Spring Data的Page界面中使用Thymeleaf的#lists.isEmpty?

时间:2016-12-07 14:20:20

标签: spring-mvc pagination spring-data thymeleaf

我使用jpenren的Thymeleaf Spring数据方言:https://github.com/jpenren/thymeleaf-spring-data-dialect

遵循他的最后建议:

  

默认情况下,SpringDataDialect在请求中搜索属性   "页面"或者如果是一个类型的属性   org.springframework.data.domain.Page存在。使用其他模型   属性,使用sd:page-object =" $ {attrName}"

我在Spring控制器中做了类似的事情:

@RequestMapping("search")
public String search(Model model, @PageableDefault Pageable pageable) {
    Page<User> users = userRepository.findAll(pageable);
    model.addAttribute("users", users);
    return "user/search";
}

在我的search.html视图中,这里有一段摘录:

    <table class="table">
      <caption class="text-xs-center" th:if="${#lists.isEmpty(users)}">No user found</caption>
      <thead>
        <tr>
          <th>Username</th>

            (...)

      <tbody>
        <tr th:each="user : ${users}">
          <td th:text="${user.name}">Username</td>

不幸的是${#lists.isEmpty(users)}不起作用。它适用于我没有使用此Page<?>的其他页面。

那我该怎么做这个测试?

1 个答案:

答案 0 :(得分:2)

看起来Thymeleaf的#lists确实期望List明显不是Page,因为它只实现Iterable。您可以参考page.content获取列表内容。

由于您首先要检查内容的存在(或不存在),因此您可以直接使用Page.hasContent(),这意味着th:unless="{adherents.hasContent()}也可以执行此操作。< / p>