【问题标题】:Thymeleaf th:switch with th:eachThymeleaf th:switch 与 th:each
【发布时间】:2018-05-09 01:57:13
【问题描述】:

在以下情况下,如何将 th:each 应用于 th:switch 以获取来自 Enum 的值?

public enum Framework {
    ABC0(0, "Name0"),
    ABC1(1, "Name1"),
    ABC2(2, "Name2"),
    ABC3(3, "Name3");

    public int id;
    public String name;

    private Framework (int id, String name){
            this.id = id;    
            this.name = name;
    }
}

在模型类中我有一个字段:

private int frameworkId;

谢谢

【问题讨论】:

    标签: java spring-mvc thymeleaf


    【解决方案1】:

    我想通了 - 这很简单。

    我在Controller类中传递了Framework的值:

    model.addAttribute("frameworks", Framework.values());
    

    然后:

    <span class="cls">Framework:</span>
    <span th:switch="${item.frameworkId}">
        <span th:each="fw : ${frameworks}">
            <span th:case="${fw.id}" th:text="${fw.name}"></span>
        </span>             
    </span>
    

    【讨论】:

      猜你喜欢
      • 2018-05-11
      • 2021-06-10
      • 2018-01-07
      • 2023-03-31
      • 2015-06-17
      • 2020-10-09
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      相关资源
      最近更新 更多