英文:
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't seem to work. If however, I put the Motherboard enum into the Types Class, it does... Is there a way I'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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论