SpringMVC form:options items attribute: what exactly is it expecting?

前端 未结 2 1773
野趣味
野趣味 2021-02-19 20:24

I\'m still new to SpringMVC (and jstl for that matter). I\'m trying to populate options in a select from a list of objects. I\'ve found a way to do it using c:forEach, but I k

2条回答
  •  我在风中等你
    2021-02-19 20:47

    The Spring Documentation says this about the items attribute of the form:options tag:

    The items attribute is typically populated with a collection or array of item objects. itemValue and itemLabel simply refer to bean properties of those item objects, if specified; otherwise, the item objects themselves will be stringified. Alternatively, you may specify a Map of items, in which case the map keys are interpreted as option values and the map values correspond to option labels. If itemValue and/or itemLabel happen to be specified as well, the item value property will apply to the map key and the item label property will apply to the map value.

    In a nutshell, if you need to use a List of your Custom Beans as the items attribute you need to use also the itemValue and itemLabel attributes. Personally, I'll prefer using Maps -LinkedHashMap instances speciffically- for populating my select tags, but that's a matter of taste.

    Adapting an example from the Spring Documentation, your code should look like this:

     
          
          
     
    

    I'm using the company attribute as both itemValue and itemLabel, but you're free to choose the attributes that fit your requirements.

提交回复
热议问题