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>
Related
I am working on a Spring MVC web app and I'm trying to figure out a way to display a property of type Set that is part of the entity in question. For the rest of the properties (String, boolean, int, etc) I can get away with the JSP file just using something like this to display them :
<li>
<label for="active">Active : </label>
<form:checkbox path="active" id="active" disabled="true"/>
<span><form:errors path="active" cssClass="error"/></span>
</li>
However, for a property of the entity which is a Set of other entities I cannot figure out how to display it. I'm thinking of using some sort of sub-table but I fiddled with it a lot with no success. I tried searching google and can't quite find what I need.
Thanks!
* New problem, but getting there! *
My OrderedMed class has a property of type StrengthUnit and I'm able to display its name using the method in the comments below :
<li>
<label for="meds[${loop.index}].strengthUnit.name">Strength Units : </label>
<form:input path="meds[${loop.index}].strengthUnit.name"/>
<form:errors path="meds[${loop.index}].strengthUnit.name" cssClass="error" />
</li>
However, I need this to be a drop-down that the user can change. My Med entity also has a property of type StrengthUnit and I'm able to accomplish what I want in its respective edit page by sending a list of all Strength Units from the controller and using the following code :
<select name="strengthUnit" path="strengthUnit.name" id="strengthUnit">
<option value="0" ${med.strengthUnit eq null ? 'selected' : ''}></option>
<c:forEach items="${strengthUnits}" var="strengthUnitSingle">
<option value="${strengthUnitSingle.id}" ${med.strengthUnit.name eq strengthUnitSingle.name ? 'selected' : ''}>${strengthUnitSingle.name}</option>
</c:forEach>
</select>
I'm trying to do the same thing with this entity and I cannot seem to make it work using the method suggested in the comments. I'm trying the below code :
<select name="strengthUnit" path="strengthUnit.name" id="strengthUnit">
<option value="0" ${meds[loop.index].strengthUnit eq null ? 'selected' : ''}></option>
<c:forEach items="${strengthUnits}" var="strengthUnitSingle">
<option value="${strengthUnitSingle.id}" ${meds[loop.index].strengthUnit.name eq strengthUnitSingle.name ? 'selected' : ''}>${strengthUnitSingle.name}</option>
</c:forEach>
</select>
Now i am getting this exception again :
javax.el.PropertyNotFoundException: Property '0' not found on type mdhis_webclient.entity.OrderedMed
I am using the same method to access the Set's index, what am I doing wrong?
Thanks!
You will probably need a forEach loop with the varStatus, example:
<c:forEach var="yourVar" items="${yourList}" varStatus="loop" >
<label for="yourVar[${loop.index}].something">Something ${loop.index + 1}</label>
<form:input path="yourVar[${loop.index}].someting" cssClass="form-control" autocomplete="off" />
<form:errors path="yourVar[${loop.index}].something" cssClass="text-danger" />
</c:forEach>
For the second part of my question, this is what i needed! I just needed a toString() method to my StrengthUnit entity (they should all have one anyway!)
<form:select name="strengthUnit" path="meds[${loop.index}].strengthUnit.name" id="strengthUnit">
<form:option value="0" label=""></form:option>
<form:options items="${strengthUnits}" />
</form:select>
I am using Spring form tag library & a select jquery plugin
An error occurs on running the jsp
org.apache.jasper.JasperException: /WEB-INF/view/home.jsp(51,4)
/WEB-INF/view/createpost.jsp(19,15) equal symbol expected
<li>
<form:select name="mood" class="selectpicker" data-style="btn-sm"
multiple data-width="100px" data-size="5" //Line 19
title='Feeling' path="mood">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</form:select>
</li>
<li>
<form:select name="Working" class="selectpicker" data-style="btn-sm"
data-width="110px" data-size="5" title='WorkingAt ..' path="WorkingAt">
<option value="1">A</option>
</form:select>
</li>
When I remove the multiple attribute from the first select tag, error disappears, but if effects the functionality. multiple attribute enables multiple value selection in the select tag.
Seems like spring form is not allowing multiple attribute to run.
Can anyone help on this?
I had the same issue when i try to add disebbled selected disabled after adding it like below problem got solved.
<form:option value="0" label="Membership Type" selected="true" disabled="true"/>
for your answer,
<option value="1" multiple="true">A</option>
i have one struts 2 select tag like below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc"></s:select>
it rendered as below
<select data-inputs="myListInput" >
<option value="myListVal1" >myListDesc1</option>
<option value="myListVal2" >myListDesc2</option>
<option value="myListVal3" >myListDesc3</option>
</select>
i want to add one more option field with null value
<option value="" ></option>
to the Option field with modification in the list.
In struts 1 it is possible like below
<html:option value=""></html:option>
like that is it possible in the struts 2
note:i couldn't able to modifiy the list before the page load in the java
There is an Attribute called emptyOption in the select tag.
It worked as below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc" emptyOption="true" ></s:select>
if you want to add one more option field with null value at the begining of the list you can use headerKey & headerValue attribute of s:select tag.
Check example given below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc" headerKey="0" headerValue="" ></s:select>
So the first element of your list will be empty & it's value will be 0(zero) here.
It will be render as
<select data-inputs="myListInput" >
<option value="" >0</option>
<option value="myListVal1" >myListDesc1</option>
<option value="myListVal2" >myListDesc2</option>
<option value="myListVal3" >myListDesc3</option>
</select>
You can set any value on place of 0 (zero).
Hope this will help you.
Could anyone please explain me how to avoid the duplicate enters of records in to the select box options during editing the page.I am using java to develop web application I am very new to that,I need to remove the duplicates from the select box option while editing.Could anyone please help me .
Sample Code
<td><select property="employeestatus" value="" id="employeestatus">
<c:if test="${employee.employeestatus!=null}">
<option value="${employee.employeestatus}">${employee.employeestatus}</option>
</c:if>
<option value="">Select</option>
<option value="Active">Active</option>
<option value="Terminated">Terminated</option>
<option value="Deceased">Deceased</option>
<option value="Resigned">Resigned</option>
<option value="InActive">InActive</option>
</select>
<html:errors property="employeestatus"/>
<p id="employeestatus" style="color:red"/>
</td>
I would suggest to add the selected attribute on the option corresponding to your "employeestatus" variable instead of duplicating this option on the first line.
See HTML option selected Attribute
By the <html:errors ...>, I gess you use Struts 1. If it's true, I would use the
<html:select ...> tag and
<html:options or optionsCollection ...>
with java Map store in some context (servlet, session or request) to configure options. That struts tag will select the good option corresponding to your variable value.
See Struts <html:select ...> doc
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}"/>