【问题标题】:How to print out elements from the List grouped by alphabet letters in Thymeleaf?如何打印出在 Thymeleaf 中按字母分组的列表中的元素?
【发布时间】:2021-06-11 11:56:17
【问题描述】:

我有以下数据库表:

CREATE TABLE authors
    (
        id         INT AUTO_INCREMENT PRIMARY KEY,
        authorName VARCHAR(250) NOT NULL
    );

我想打印我们的作者如下:

一个

  • 安塞尔玛·麦肯南

B

  • 伯蒂·甘德利
  • 贝特-安·阿斯凯姆

C

  • 牧师贾科维奇
  • 克里斯蒂·艾萨克森

D

  • 丹尼·格莱德
  • 达西·侯麦
  • Dom Robuchon

目前我通过thymleaf模板打印如下,不想每次都在作者面前打印出Alphabet LETTER:

一个

  • 安塞尔玛·麦肯南

B

  • 伯蒂·甘德利

B

  • 贝特-安·阿斯凯姆

C

  • 牧师贾科维奇

C

  • 克里斯蒂·艾萨克森

D

  • 丹尼·格莱德

D

  • 达西·侯麦

D

  • Dom Robuchon

我的控制器:

@GetMapping("/index.html")
    public String mainPage(Model model) {
        model.addAttribute("authorsData", bookService.getAuthorData());
        return "/authors/index";
    }

我的服务:

public List<Author> getAuthorData() {
    List<Author> authors = jdbcTemplate.query("SELECT id, authorName from authors ORDER BY authorName", (ResultSet rs, int rowNum) -> {
        Author author = new Author();
        author.setId(rs.getInt("id"));
        author.setAuthorName(rs.getString("authorName"));
        return author;
    });
    return new ArrayList<>(authors);
}

我的 Thymeleaf 代码如下:

<div class="Authors-block" th:each="author : ${authorsData}" >
  <h2 class="Authors-title" id="a" th:text="${author.getAuthorName().charAt(0)}">
  </h2>
  <div class="Authors-letter">
    <div class="Authors-item"><a href="/authors/slug.html" th:text="${author.getAuthorName()}"></a>
    </div>

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    让您的 getAuthorData() 返回 Map,其中键是字母,值是以该字母开头的作者

    public List<Author> getAuthorData() {
        List<Author> authors = jdbcTemplate.query("SELECT id, authorName from authors ORDER BY 
        authorName", (ResultSet rs, int rowNum) -> {
            Author author = new Author();
            author.setId(rs.getInt("id"));
            author.setAuthorName(rs.getString("authorName"));
            return author;
        });
        return authors.stream()
           .collect(
              Collectors.groupingBy(
                 author -> author.getAuthorName().charAt(0)
              )
           );
    }
    

    在 html 中,您可以遍历地图条目,如下所示:How to loop through Map in Thymeleaf 请注意,作为一个值,您将拥有可迭代的数组

    【讨论】:

    • 非常感谢!这里的流有很大帮助。
    【解决方案2】:

    您也可以进行收藏选择。例如这样的事情:

    <div class="Authors-block"
         th:each="letter : ${ {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'} }"
         th:with="authors=${authorsData.?[authorName.startsWith(#root.letter)]}"
         th:unless="${authorsData.?[authorName.startsWith(#root.letter)].empty}"
         >
      <h2 class="Authors-title" id="a" th:text="${letter}" />
      <div class="Authors-letter">
        <div class="Authors-item" th:each="author: ${authors}"><a href="/authors/slug.html" th:text="${author.getAuthorName()}"></a>
        </div>
      </div>
    </div>
    

    但我不知道我是否会推荐它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多