在使用Thymeleaf和Spring Boot的嵌套类内部使用枚举:

huangapple go评论106阅读模式
英文:

Using an enum within a nested class with Thymeleaf and Spring Boot

问题

我有以下的类

Types.java

```java
public class Types {
    public static class PC {
        public static enum Motherboard {
            OPTION1("选项1"),
            OPTION2("选项2"),
            OPTION3("选项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 中使用它们的方法?


<details>
<summary>英文:</summary>

I have the following class:

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; }
    }
};

};


In my Thymeleaf template I have:

<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>


However, this doesn&#39;t seem to work. If however, I put the Motherboard enum into the Types Class, it does... Is there a way I&#39;m missing to be able to nest enums in classes and use them in Thymeleaf?

</details>


# 答案1
**得分**: 1

解决方案:

为了进入嵌套类,你需要在它们之间加上 $ 符号。例如:

```java
${T(jre.maintainme.utils.strings.Types$PC$Motherboard)}
英文:

SOLUTION:

In order to go into nested classes, you need to add a $ between them. I.e:

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

答案2

得分: 0

以下是翻译好的内容:

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

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

然而,IntelliJ声称无法解析Type,但IntelliJ是在撒谎!

英文:

What did work for me was the first thing you tried:

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

However, IntelliJ claims it can't resolve Type, but IntelliJ lies!

huangapple
  • 本文由 发表于 2020年10月8日 23:43:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64266061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定