Spring drop down not working - java

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.

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

Use value of a local variable as global in thymeleaf

I have a variable that is obtain inside each block in thymeleaf. I want to send that variable to a certain method in my controller. The limitation is that the variable is obtain inside a block which makes it local, not accessible global.Hence I get an error while trying to use it. How can I move inside the scope to get the variable so as I can use it global in thymeleaf.
<form th:action="#{/masomo/somo(date=${dateMpya.date})}" method="POST">
<select id="date" name="date" required="true">
<option value="none" selected disabled hidden >
Select a Date
</option>
<th:block th:each="somoChagua : ${masomoChagua}">
<option th:each="dateMpya: ${somoChagua}" th:value="${dateMpya}" th:text="${dateMpya.date}" ></option>
</th:block>
</select>
<button type="submit"><i class="fa fa-search"></i> </button>
</form>
There can be many different "dateMpya" objects for each "somoChagua".
But there is only one submit button.
So which "dateMpya" should be used for the submit button value?
I think what you are actually trying to do here is get the value of the "dateMpya" which the user selected in the drop-down. Is that right?
If that's the case there is no need to add any attributes to the submit button. You would access that value by using the name of the select element, which is "date".
EDIT: For the same reason you also need to remove the (date=${dateMpya.date}) part of the form action as well. The value selected in the drop-down will automatically be submitted under the name of the select element "date", it does not need to be specified.

jsp how to set the selected value?

I want to set the selected value of a select element from server side, but not getting idea how can I set the value, here is my code.
java code
model.addAttribute("dataSources", dataSourceRepository.findByDestinationId(sDestination.getId()));
jsp code:
<form:select onchange="refreshHistoryPage()" path="id" items="${dataSources}" itemLabel="name" itemValue="id"/>

Spring select tag generating hidden field

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

Dealing with Optional ListBox values in Spring MVC

I have a Spring MVC application. My JSP allows the user to select a value from a drop down list - this is optional - they do hot HAVE to make a selection.
On my JSP I have code similar to
<form:select path="referral.gpsurgery.id" >
<form:option value="-1" label="--- Select ---"/>
<form:options items="${gpsurgery}" itemValue="id" itemLabel="practiseName"/>
</form:select>
If the user does not make a selection, a referral.gpsurgery object is passed thru to my controller method with an id value of "-1" ( which is what I would expect ). What this actually means as far as my code is concerned is that when the data is persisted, the gpsurgery field on my referral entity should be set to null.
It feels like Spring should sort this out for me, rather than me having to look at my object graph and remove any gpsurgery instances which have an id of "-1", but I am drawing a blank on how to achieve this. It seems like such a common use case that there must be a standard way of doing it.
As also stated in this question, you can use:
<form:select path="referral.gpsurgery.id">
<form:option value=""> </form:option>
<form:options items="${gpsurgery}" itemValue="id" itemLabel="practiseName"/>
</form:select>

Categories