<form:select> items from list with default prompt - spring

The following code works correctly in my jsp:
<form:select path="propertyPath" >
<form:options items="${modelObject}" itemValue="id" itemLabel="name"/>
</form:select>
However, I want to have a default nulled value with the label "Select" as a prompt to the user. I've tried the following:
<form:select path="propertyPath" >
<form:option label="Select" value=""/>
<form:options items="${modelObject}" itemValue="id" itemLabel="name"/>
</form:select>
This populates the drop down labels correctly, however on submitting I find that the itemValues have all been set to empty. Does anyone know why these values are being removed when I add the additional field?

Hi #user2774284 can you try something like this it's another option:
<form:select path="" cssClass="" id="yourId" value="${modelObject}">
<option value="" label="Select" ></option>
<option value="yourValue" <c:if test="${modelObject == yourValue}"> selected </c:if>>yourValue</option>
<option value="yourValue" <c:if test="${modelObject == yourValue}"> selected </c:if>>yourValue</option>
<option value="yourValue" <c:if test="${modelObject == yourValue}"> selected </c:if>>yourValue</option>
</form:select>
The code above work if you knows value (yourValue) from modelObject. Finally the JSP convert JSTL to DOM HTML conventional.
I hope help you :)

Related

Using `"disabled"` with Spring `<form:options>` doesn't work

I expected to be able to use the "disabled" attribute like so:
<form:select path="supervisor.personId" class="w-50 border d-block">
<form:option value="null">Choose An Supervisor.../>
<form:options items="${supervisors}" itemValue="supervisorId" itemLabel="supervisorName" disabled="isDeleted"/>
</form:select>
where "isDeleted" is a field in the database that returns "true" when a supervisor has been marked for deletion (and therefore should not be available for selection from the dropdown). It doesn't work. The only way I've found to use "disabled" with <form:options> is to hardcode the value to "true" like so:
<form:select path="supervisor.personId" class="w-50 border d-block">
<form:option value="null">Choose An Supervisor.../>
<form:options items="${supervisors}" itemValue="supervisorId" itemLabel="supervisorName" disabled="true"/>
</form:select>
Which obviously isn't the result I desire. Has anyone encountered this before or have a suggestion as to how to work around it?

Spring form option does not select item, options does

${products} contains a List<Product>. Product is a #Entity, has an equals method that compares by id. There is no converter or formatter registered for Product (other than Spring Data's DomainClassConverter but that doesn't seem to kick in for this case):
This works:
<form:select path="productFrom">
<form:option value="" label="-" />
<form:options items="${products}" itemValue="id" itemLabel="name"/>
</form:select>
This (needed for optgroup-ing, but simplified here) does not select the correct value:
<form:select path="productFrom">
<form:option value="" label="-" />
<c:forEach items="${products}" var="product">
<form:option value="${product.id}">${product.name}</form:option>
</c:forEach>
</form:select>
After debugging SelectedValueComparator I found that it tries to compare a candidateValue of type Long to a boundValue of String. I could work this around by creating a toString() method in product that returns the id as String. (Or I could have modified the equals() method to handle Long.)
Still, I have a bad feeling that I'm doing something wrong here.
In the end I solved this by adding a new method to Product:
public String getIdString() {
return id == null ? "" : id.toString();
}
and changing the option definition:
<form:option value="${product.idString}">${product.name}</form:option>
Still not sure I'm doing this the right way, any tips are appreciated.

dijit form filteringselect not support options in EL variable?

There is a very beautiful solution in spring to populate up a select list from EL variable such as:
<form:select id="customerentity_customerTitle" path="customerTitle" cssStyle="width:300px;">
<form:option value="None" label="*** Select Your Title ***"></form:option>
<form:options items="${fn:split(title_t, ',')}" />
</form:select>
This JSTL and spring solution can be applied easily and very affection.
When I tried to find a similar solution in Dojo. I found most close solution is dijit/form/FilteringSelect, but when I try to use similar way to populate a dynamic generated dropdown select, it doesn't work.
<select id="customerentity_customerTitle" name="customerTitle" style="width: 300px;" data-dojo-type="dijit/form/FilteringSelect"
data-dojo-props="value: '${customerentity.customerTitle}',placeHolder: 'Select Your Title',options:'${fn:split(title_t, ',')}'">
</select>
My question is: Is there any possible to use similar way to do it in digit/form/FilteringSelect? or I have to do it by using javascript to populate it?
Any advice is welcome!!
Edit
title was hold in a property file and access by using:
<fmt:setBundle basename="bundles.customer-resources" />
resources bundle.
The sources looks like:
storageway.customer.person.title.options=Mr.,Ms.,Mrs.,Dr.,Other
access by:
<fmt:message key="storageway.customer.person.title.options" var="title_t" scope="session" />
It is a simple string array instead of a key:value map. Spring form can handle it properly but dijit/form/FilteringSelect not.
You can create normal option tags inside the dijit/form/FilteringSelect.
They will be rendered. So I bet you can do
<select id="customerentity_customerTitle" name="customerTitle" style="width: 300px;" data-dojo-type="dijit/form/FilteringSelect"
data-dojo-props="value: '${customerentity.customerTitle}',placeHolder: 'Select Your Title'">
<form:options items="${fn:split(title_t, ',')}" />
</select>
See the snippet:
require(["dojo/parser", "dojo/domReady!"], function(parser){
parser.parse();
});
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/resources/dojo.css">
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/tundra/tundra.css">
<div class="tundra">
<select id="customerentity_customerTitle" name="customerTitle" style="width: 300px;" data-dojo-type="dijit/form/FilteringSelect"
data-dojo-props="value: 'b',placeHolder: 'Select Your Title'">
<option value="a">value A</option>
<option value="b">value B</option>
<option value="c">value C</option>
</select>
</div>

how can i set a default value using jstl in a drop down

am getting a list of values in the below itereate logic.I need to set one particular value "Count" as a default value in my dropdown
<logic:new name="val">
<logic:iterate name="val" id="Opt" type="parameter">
<c:set var="Key" value="${fn:trim(Opt.key)}"/>
<c:choose>
<c:when test="${Key == selectedValue}">
<option value="${Key}" selected="selected" ><c:out value="${Opt.Value}" /></option>
</c:when>
<c:otherwise>
<option value="${Key}"><c:out value="${opt.Value}" /></option>
</c:otherwise>
</c:choose> </logic:iterate>
I tried as below but its not defaulting the value in drop down.Any body can suggest any other way to set the default.
<c:if test="${paramName == 'Count'}">
<option value="-"><c:out value="${defaultLabel}"/> </option>
</c:if>
I am not sure whether i understood your question correctly or not but you can set the default value before starting your iteration
<option value="-" selected="selected" >your Default Value</option>
if my understanding is wrong please help me understand your requirement.

Is it possible to use multiple params in the itemLabel of a form:select/form:option

I'm using Spring. I've got a JSP with a form:select in it displaying the list of users. In this situation the username or id won't mean much to the user so I need to show firstname lastname.
I've tried:
<form:select id="userSelect" name="userId" path="user.id">
<option value="">Select to Edit</option>
<form:options items="${user.userList}" itemValue="id" itemLabel="lastname firstname" />
</form:select>
But that gives me a big error. How can I make the itemLabels show lastname, firstname?
I don't think so. Either have a getFullName() getter in your object returning the concatenation of last and first names, or display the options one by one, in a loop:
<form:select id="userSelect" name="userId" path="user.id">
<option value="">Select to Edit</option>
<c:forEach var="theUser" items="${user.userList}">
<form:option value="${theUser.id}"><c:out value="${theUser.lastname} ${theUser.firstname}"/></form:option>
</c:forEach>
</form:select>
With concatenation performed by user.getFullName():
<form:select path="user"
items="${user.userList}"
itemValue="id"
itemLabel="fullName">
</form:select>

Resources