【问题标题】:Using an enum within a nested class with Thymeleaf and Spring Boot在 Thymeleaf 和 Spring Boot 的嵌套类中使用枚举
【发布时间】:2021-01-23 17:01:59
【问题描述】:

我有以下课程:

Types.java:

public class Types {
    public static class PC {
        public static enum Motherboard {
            OPTION1("option 1"),
            OPTION2("option 2"),
            OPTION3("option 3");

            private final String displayValue;

            private Motherboard(String displayValue) { this.displayValue = displayValue; }

            public String getDisplayValue() { return this.displayValue; }
        }
    };
};

在我的 Thymeleaf 模板中,我有:

<select name="select-motherboard">
    <option th:each="size : ${T(jre.maintainme.utils.strings.Types.PC.Motherboard).values()}" th:value="${size}" th:text="${size.displayValue}"></option>
</select>

但是,这似乎不起作用。但是,如果我将 Motherboard 枚举放入 Types 类中,它确实...有没有一种方法可以将枚举嵌套在类中并在 Thymeleaf 中使用它们?

【问题讨论】:

  • 嗯。那应该工作得很好。如果你运行 System.out.println(OPTION1.getClass()) 会发生什么?
  • 它带有class jre.maintainme.utils.strings.Types$PC$Motherboard 编辑:想通了。我最初使用 # 来区分类,还有 .但正确的是刚才指出的是在类之间使用 $。
  • 在 Stack Overflow 上,不要用正确的答案编辑你的问题,而是发布你自己问题的答案(你甚至可以接受它)。

标签: java spring spring-boot thymeleaf


【解决方案1】:

对我有用的是你尝试的第一件事:

${T(com.my.package.path.Client.Type).values()

然而,IntelliJ 声称它无法解析 Type,但 IntelliJ 谎言

【讨论】:

    【解决方案2】:

    解决方案:

    为了进入嵌套类,您需要在它们之间添加一个 $。即:

    ${T(jre.maintainme.utils.strings.Types$PC$Motherboard)}
    

    【讨论】:

    • 这个解决方案对我不起作用。我有一个带有内部枚举类型的客户端类。我尝试使用 ${T(my.package.path.Client$Type)} 访问,它不会编译。 :S
    猜你喜欢
    • 2011-12-14
    • 2017-08-24
    • 2011-11-09
    • 1970-01-01
    • 2019-09-12
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    相关资源
    最近更新 更多