Spring select tag generating hidden field

2019-08-06 20:08发布

问题:

I am not very familiar with the Spring tag and seems like i am struck in some issue which i am not able to understand as of now.

I have displaying two select tags in my jsp and they are backed by an Arraylist and map here is the code for them

<form:select path="prsBTOData[${status.index}].colors" items="${prsBTOData.colors}" 
cssClass="productDetailsSelect"/>

and

<form:select path="prsBTOData[${status.index}].fonts" items="${prsBTOData.fonts}" 
cssClass="productDetailsSelect" >

colors is being backed by Array list while the fonts is being backed by the Map.below is the generated HTML

<select multiple="multiple" class="productDetailsSelect" name="prsBTOData[0].colors" 
    id="prsBTOData0.colors">
     <option selected="selected" value="Red">Red</option>
     <option selected="selected" value="Green">Green</option>
     <option selected="selected" value="Black">Black</option>
</select>
<input type="hidden" value="1" name="_prsBTOData[0].colors">

i am not sure why its doing multiple="multiple" and not showing any drop down but only showing the RED as selected value, while i was expecting a list with drop down options. even not sure why this hidden field is getting generated and what is its purpose?

回答1:

In form:select the items attribute is the list of items that needs to be displayed in select box. And path attribute is the property that is bound with the selected value.

As you have given an arraylist (having multiple values) as the path, spring assumed you wanted a multiple value selected drop down.

You might want to give like this (assuming you have color property for prsBTOData )

<form:select path="prsBTOData.color" items="${prsBTOData.colors}"/>

And consider using separate model objects for maintaining static/reference data (colors,fonts) as below:

<form:select path="prsBTOData.color" items="${referenceData.colors}"/>