4

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?

3
  • Huh. That should work just fine. What happens if you run System.out.println(OPTION1.getClass())? Oct 8, 2020 at 16:02
  • 1
    It comes out with class jre.maintainme.utils.strings.Types$PC$Motherboard EDIT: Figured it out. I was using # originally to distinguish the classes, and also . but the correct was as that just pointed out is to use $ between the classes.
    – JamieRhys
    Oct 8, 2020 at 16:06
  • On Stack Overflow, instead of editing your question with the correct answer, post an answer to your own question (you can even accept it). Oct 8, 2020 at 17:37

2 Answers 2

1

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)}
1
  • This solution didn't work for me. I've got a Client class with an inner enum Type. I tried accessing with ${T(my.package.path.Client$Type)} and it won't compile. :S Sep 27, 2021 at 20:26
0

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!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.