Dealing with Optional ListBox values in Spring MVC - java

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>

Related

Spring form:options tag behaving differently

in my jsp I list Projects and Home Office states. Projects is a nice drop-down that is collapsed, but Home Office drop-down shows first several entries. Why does Home Office drop-down act differently than Projects? I want it to be collapsed like Projects. getProjects() returns a List of Project objects and getHomeOffice() returns a List of String objects.
<p>
Project: <form:select path="project">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${filterBoard.getProjects()}" />
</form:select>
</p>
<p>
Home Office: <form:select path="homeOffice">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${employee.getHomeOffice()}" />
</form:select>
</p>
I figured it out. I needed to change the data type of the homeOffice field in my java class from List to String.
The "project" field that corresponds with <form:select path="project"> is a String in my java class and filterBoard.getProjects() returns a List. And that works. I had both the "homeOffice" field and employee.getHomeOffice() as Lists. I changed the "homeOffice" field to be a String and renamed my getHomeOffice() method to getHomeOfficeList() and still had it return a List and that fixed it.

JSP Class Explanation

So recently I have run into an error that I have never encountered.
The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit
I set the model attribute in the controller, granted the object is fairly large I wouldn't expect for it to throw an error. I started by limiting the code to figure out what was causing the issue. I have read here to remove the <jsp:include and use <%# include.
Next step I took was to break up the JSP. I realize now that I am building out my Colors and Fonts individually like this
<form:select path="bottom.backgroundColor" class="form-control">
<form:option value="#000000">Black</form:option>
<form:option value="#FFFFFF">White</form:option>
<form:option value="#FF0000">Red</form:option>
<form:option value="#00FF00">Green</form:option>
<form:option value="#0000FF">Blue</form:option>
<form:option value="#F0F000">Yellow</form:option>
<form:option value="#FF7000">Orange</form:option>
<form:option value="#600060">Purple</form:option>
<form:option value="#905030">Brown</form:option>
</form:select>
Removing these items has fixed my issues.
QUESTION: Why? My understanding is that a JSP builds out its own class however it doesn't seem like it took much to break it. Is the problem the size of the modelAttribute that was passed in? It sounds like others have solved issues with loops and such. Can anyone point me to a resource or explain this better? I can supply more code if necessary.
The problem is that your JSP is too large, even after reducing several parts of it. Seems that you still are repeating this part of the JSP code several times (or similar):
<form:select path="bottom.backgroundColor" class="form-control">
<form:option value="#000000">Black</form:option>
<form:option value="#FFFFFF">White</form:option>
<form:option value="#FF0000">Red</form:option>
<form:option value="#00FF00">Green</form:option>
<form:option value="#0000FF">Blue</form:option>
<form:option value="#F0F000">Yellow</form:option>
<form:option value="#FF7000">Orange</form:option>
<form:option value="#600060">Purple</form:option>
<form:option value="#905030">Brown</form:option>
</form:select>
which in the end is fill the _jspService method in the auto generated servlet. A solution for this problem is to store this static data in application scopde a.k.a. ServletContext, and fill the values of your <form:option>s from it.

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.

<form:options tag spring mvc binding

I am binding a list with Spring MVC form select box like this given below
<c:forEach var="item" items="${instance.parameters}" varStatus="itemsRow">
<form:select size="6" path="parameters[${itemsRow.index}].parmvalues" multiple="true">
<form:options items="${item.pluginParmvalues}" itemValue="id" itemLabel="parmValue" />
</form:select>
</c:forEach>
After binding this <form:select displays values correctly. But when I try to save it it throws exception. Should I also bind <form:options values to something like this
<form:options path="parameters[${itemsRow.index}].parmvalues[0].parmValue"
instead of items="${item.pluginParmvalues}" to get/save values in controller?
Exception:
Cannot convert value of type [java.lang.String] to required type [com.domain.Parmvalue] for property 'parmvalues[0]': no matching editors or conversion strategy found]
This way select box is not being bounded. You have to take a hidden field and bind that hidden field with path="parameters[${itemsRow.index}].parmvalues". Each time select box value is changed. Change value of that hidden field too through javascript/jquery.

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