spring-form:options tag with enum

前端 未结 3 1721
耶瑟儿~
耶瑟儿~ 2021-02-20 10:24

I\'m having troubles displaying a dropdown list with the proper values. I\'m using the , and

3条回答
  •  灰色年华
    2021-02-20 10:47

    If you created a spring controller and you want to pass an enum to your jsp page, you can do it like this:

    Enum example:

    public enum Coin {
    
        HEADS("Heads", "heads"),
        TAILS("Tails", "tails");
    
        private final String fullName;
        private final String shortName;
    
        private Coin(String fullName, String shortName) {
            this.fullName = fullName;
            this.shortName = shortName;
        }
    
        public String getFullName() {
            return fullName;
        }
    
        public String getShortName() {
            return shortName;
        }
    
    }
    

    Passing this enum to your model:

    model.addObject("coins", Coin.values());
    

    Using it in your jsp page with form:

    
        
    
    

提交回复
热议问题