spring-form:options tag with enum

前端 未结 3 1633
耶瑟儿~
耶瑟儿~ 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:44

    You do not even need to use items attribute if you are using <spring-form:options> tag.
    The options tag:
    Renders a list of HTML 'option' tags. Sets 'selected' as appropriate based on bound value.
    References Spring form tld. So for your need I guess falling back to JSTL core with <spring-form:option/> will suffice.

    0 讨论(0)
  • 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:

    <form:select path="selection">
        <form:options items="${coins}" itemValue="shortName" itemLabel="fullName" />
    </form:select>
    
    0 讨论(0)
  • 2021-02-20 11:01

    It looks like the solution to this problem was that I was using the attribute "path" in the <spring-form:options> tag. It should have been "items", not "path".

    the corrected JSP fragment:

    <spring-form:select path="selectOptions">
        <spring-form:option value="" label="*** Select Option ***" />
        <spring-form:options items="${availableOptions}" />
    </spring-form:select>
    
    0 讨论(0)
提交回复
热议问题