Using `"disabled"` with Spring `<form:options>` doesn't work - drop-down-menu

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?

Related

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.

Spring form tags. Enum i18

I'm using Spring form tag library to automatically bind values from enum in my Model to FORM fields:
<form:select path="status">
<form:options/>"
</form:select>
status is enum field in my form-backing object:
public enum Status {ON, OFF}
But in <select> tag I get labels like ON and OFF. Is there any way to localize this labels?
I know this is an old post but I found myself in this situation today. I didn't find any built-in solutions. A quick solutions is the following:
<form:select path="my.field">
<c:forEach items="${enumValues}" var="type" >
<form:option value="${type}">
<spring:message code="some.key.${type.name.toLowerCase()}" />
</form:option>
</c:forEach>
</form:select>
If you need this more than once you will need to create your own tag.

<form:select> items from list with default prompt

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 :)

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>

Spring MVC - Reference Data

Here is the scenario: I have something like this..
<form:select path="somePath" .....>
<form:option value="" label="Please Select"/>
<form:options items="${students}" itemValue="id" itemLabel="name"/>
</form:select>
This dropdown list works fine.
But how can I display name of a particular student? I wan to do something like this:
<c:out value="${students[id].name}"/>
Can any one help me with the syntax?
Thanks
I assume that ${students} is an array or list of student objects. As such, it's not indexed by id and can't be directly accessed that way.
Options include:
1) Include your collection of students as a map from id to student object; your items attribute then becomes ${students.values}, and you can then look up an individual student as ${students[id]}.
2) Or, keep it as a list and then iterate through your list and find the one where the id matches:
<c:forEach var="student" items="${students}">
<c:if test="${student.id==id}">
<c:out value="${student.name}" />
</c:if>
</c:forEach>
3) Or, lastly, if you know from the beginning which student you care about, include that student separately in the reference data.

Resources