How to display all possible enum values in a dropdown list using Spring and Thymeleaf?

前端 未结 3 1874
忘了有多久
忘了有多久 2020-12-30 22:51

I have a domain object that has an enum property and I want to display a dropdown list with all possible enum values in the form for this object. Imagine the following objec

3条回答
  •  北海茫月
    2020-12-30 23:15

    In addition, if you want to separate the enum ordinal name from the string displayed in the GUI, add additional properties, for example a displayName:

    public static enum State {
    
        OPEN("open"),
        IN_WORK("in work"),
        FINISHED("finished");
    
        private final String displayName;
    
        State(String displayName) {
            this.displayName = displayName;
        }
    
        public String getDisplayName() {
            return displayName;
        }
    }
    

    And in the html file:

    
    

    This will present the displayName to the user and allows you to silently change this strings later without refactoring the code. You may add more properties like th:title this way.

提交回复
热议问题