Spring select tag generating hidden field - java

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?

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}"/>

Related

How to set default value in Select dropdown in velocity template file?

I am trying to build a custom build Jira Plugin, on the UI of plugin I am displaying select dropdown which consist of list of Issue Types.
I need to make option value "ALL" as default one for that select dropdown. I have written following code for that.
<label for="issue-types" >Issue Type(s):</label>
<select onchange="disableIfNoIssueTypeSelectedMandate()" class="multi-select" multiple="multiple" id="issue-types-mandate" name="issueTypes[]">
#foreach ($issueType in $allIssueTypesInProject)
<option value="$issueType.get("id")">$issueType.get("name")</option>
#end
</select>
</div>```
Can anyone please help me to know how to set default value in this select dropdown?
probably you should use <option selected="selected" value="$issueType.get("id")">$issueType.get("name") but with the default string values that you need

Spring mvc and dynamic submit form

I am trying to learn Spring mvc and JSP, and I have a problem that how I can get the value of select list dynamically?
If I have a select list:
<select name="qty>
<option>1</option>
</select>
and in the spring mvc, I can get the it value by using #RequestParam("qty") int qty. However, If I surround the select list with a loop:
<c:forEach begin="1" end="10">
<select name="qty>
<option>1</option>
</select>
</c:forEach>
I am thinking of using an array #RequestParam("qty") int[] qty but I think it is not a good option because well the form is much more complicated that this example, and assume that the qty is associate with a specific item. So how can get the qty of the item which the user has selected during submitting the form?

how to get values of several dropdown lists from jsp to servlet

i have a question when i try to capture the value of dropdown list from jsp file to servlet.
<select name="extraroom">
<c:if test="${booking.roomType ne 'single'}">
<c:forEach var="i" begin="0" end="${booking.count}">
<option value="<c:out value="${i}"/>"><c:out value="${i}"/></option>
</c:forEach>
</c:if>
</select>
i use Integer.parseInt(request.getParameter("extraroom"))
i have many drop down lists according to different roomType.
but the result in java servlet always get the value of the first dropdown lists...
what should i change in servlet?
Thank you.
For a select to be retrieved by a server the select has to
be inside a form and
have a unique name identifier inside (it; best the identifier is unqiue across the entire DOM tree)

Spring drop down not working

i am using spring mvc,
<form:select class="form-control" id ="cmsphyexamtesttype_cmsPhysicalExamCategory_id" path="cmsPhysicalExamCategory.id">
<form:option value="0" label="--- Please select the Category ---"/>
<form:options items="${cmsphyexamtestcategorys}" itemLabel="name" itemValue="id" />
</form:select>
html code
<select id="cmsphyexamtesttype_cmsPhysicalExamCategory_id" name="cmsPhysicalExamCategory.id" class="form-control">
<option value="0">--- Please select the Category ---</option>
<option value="2">Genaral</option><option value="3">EYE</option><option value="4">HENT</option><option value="5">CHEST</option>
</select>
this one is working fine with new form , but in edit mode,it is not working do you have any idea it gives selected value while rendered to editing mode, i am new to spring mvc, is there any thing wrong with this code?
You are path binding the drop down value to the
cmsPhysicalExamCategory.id
So, the selected value will be retained in the form when you recall the form again for some other operation like 'edit.
You can change the value in the drop down and the new value will be path binded to the model. Here, its not getting binded. It may be because of the error in path bind/form submit.
Please post the form submit code/controller code for more help.
From your question what I get is that you have drop down and on submitting again , you want to render the same form, you are getting the change value but drop down value still persist to the default , first attribute of drop down, and you want to have the attribute same as value, in case this is the elaborated issue, you need to explicitly check in list for what value you want to select and set the attribute as Selected.

combo box in spring web mvc

I am using spring web mvc for my app's UI part..
By using following code, i am getting List Box where i can select more then 1 value..
<form:select path="domainsList">
<form:options items="${domainsList}" itemValue="domain" itemLabel="domain"/>
</form:select>
But I need a drop down combo box...
Can any one suggest how can i convert it to combo box ?
Thanks in advance..
Sorry, for asking silly question.. But i got working combo box by following code :
<form:select path="domainsList" multiple="false" size="1">
<form:options items="${domainsList}" itemValue="domain" itemLabel="domain"/>
</form:select>
</form:form>
Spring will decide the type of field to use depending on the type of data, so if the 'path' field is an object, it will show a dropdown, but if it is a "list" (array, collection, ...) it will show a list, unless you specify multiple="false"
This will show a list with multiple selection:
Integer[] ids;
<form:select path="ids" items="${whatever}" />
This will show a dropdown with single selection:
Integer id;
<form:select path="id" items="${whatever}" />
This will also show a dropdown with single selection:
Integer[] ids;
<form:select path="ids" items="${whatever}" multiple="false" />
The spring "form:select" tag just wraps the HTML select element. It also has an attribute size which has to be set to a value of 1 to let this selection become rendered as a combobox (in most browsers).
this is basic HTML:
http://www.w3.org/TR/html4/interact/forms.html#adef-size-SELECT
<form:select path="domainsList" size="1">
<form:options items="${domainsList}" itemValue="domain" itemLabel="domain"/>
</form:select>
#Nirmal please check your markup. This should work.
<html>
<SELECT name="selection" size="1">
<OPTION selected label="none" value="none">None</OPTION>
<OPTION label="1" value="1">OPTION 1</OPTION>
<OPTION label="2" value="2">OPTION 2</OPTION>
<OPTION label="3" value="3">OPTION 3</OPTION>
</SELECT>
</html>

Categories