Use <form:select> tag with a map

后端 未结 2 1252
误落风尘
误落风尘 2020-12-29 11:42

Is there a way to map the data inside a map to tag? I have a map Map in my code. Is there a way to map the option labels to the St

相关标签:
2条回答
  • 2020-12-29 12:15

    See the Spring form:select and form:options documentation. Use items, itemValue, and itemLabel as needed.

    <form:select path="myFormVariable">
        <form:option value="0" label="Select One" />
        <form:options items="${myCollection}" itemValue="propertyToUseAsValue" itemLabel="propertyToUseAsDisplay" />
    </form:select>
    
    0 讨论(0)
  • 2020-12-29 12:22

    The <form:options> tag supports what you want right out of the box, using the items attribute. You can do something like this:

    LinkedHashMap<Integer, String> states = new LinkedHashMap<Integer, String>();
    states.put(1, "Alabama");
    states.put(2, "Alaska");
    states.put(3, "Arizona");
    states.put(4, "Arkansas");
    states.put(5, "California");
    

    And so on. Then in your form:

    <form:select path="state">
        <form:options items="${states}" />
    </form:select>
    

    That will be rendered to something like:

    <select name="state">
        <option value="1">Alabama</option>
        <option value="2">Alaska</option>
        <option value="3">Arizona</option>
        <option value="4">Arkansas</option>
        <option value="5">California</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题