the value of form:select didn't get selected from controller - spring

i set the value of form:select but in the result it didn't get selected.
My controller:
contactBF.setNom(contact.getNom());
contactBF.setQualite("130");
model.put("contact", contactBF);
model.put("qualities", [...]);
My jsp page:
<form:form id="contactform" modelAttribute="contact" action="">
<form:input path="nom" type="text"/>
<form:select path="qualite">
<option value=""> ... </option>
<c:forEach items="${qualities}" var="qualite" >
<option value="${qualite.id}" >${qualite.nom}</option>
</c:forEach>
</form:select>
</form>
the form:input get fill with the right value and form:select is loaded with all item but the right value is not selected !
i know this solution work:
<option value="${qualite.id}" ${(qualite.id == contact.qualite) ? 'selected' : ''}>${qualite.nom}</option>
But it wil be a lot of test.

i replace :
<c:forEach items="${qualities}" var="qualite" >
<option value="${qualite.id}" >${qualite.nom}</option>
</c:forEach>
By :
<form:options items="${qualiteList}" itemValue="id" itemLabel="nom" />
And it's work ;)

Related

How can we add 'required' attribute to a dropdown box in jsp page

How can we add required attribute to a dropdown box in a JSP page? I added it in normal fields using Required = required keyword. But in dropdown, it is not working.
<div class="form-group row">
<div class="col-md-3">
<label class="form-control-label" for="customer_category">Category</label>
</div>
<div class="col-md-9">
<form:select class="select2" id="customer_category" path="category.id">
<form:option value="0">Select a Category</form:option>
<c:forEach items="${listCustomerCategory}" var="category">
<form:option value="${category.id}">${category.name}</form:option>
</c:forEach>
</form:select>
</div>
</div>
Finally i found the answer, I just Changed this line
<form:option value="0">Select a Category</form:option>
to
<option value="">Select a Category</option>
Now it is working..

Setting First Option in <Select>

I'm using Spring Forms to display a list of countries using <form:select /> tag. The list is sorted alphabetically but I'd like to place "United States" as the first option, but not selected by default
The rendered HTML is as follows (the values are the country IDs, & the IDs will never change)
<select id="countries">
<option value="1">Argentina</option>
<option value="2">Columbia</option>
<option value="3">United States</option>
</select>
Could this be accomplished with a Spring Expression? Thanks much!
Try this :)
<form:select path="countries" id="countries">
<c:forEach var="countries" items="${countriesList}">
<form:option <c:if test="${countries.id==3}"> selected="true" </c:if> value="${countries.id}"> ${countries.name}
</form:option>
</c:forEach>
</form:select>

Displaying data once using JSTL

I have a model object which has the ff. properties:
Group
Sub group
name
The object is on a List<Model> which I iterate like this:
<c:forEach var="itemBean" items="${ itemBeanList }">
<option value="${ itemBean.group }">${ itemBean.group }</option>
<option value="${ itemBean.subGroup }">${ itemBean.subGroup }</option>
<option value="${ itemBean.name }">${ itemBean.name}</option>
</c:forEach>
But I want to display the group and subGroup only once. Is their a function or a way to do this in JSTL?Or can you suggest a way to do this?
You can achieve this using varStatus attribute in forEach and c:if jstl tag as:
<c:forEach var="itemBean" items="${itemBeanList" varStatus="itemStatus">
<c:if test="${itemStatus.count == 1}">
<option value="${ itemBean.group }">${ itemBean.group }</option>
<option value="${ itemBean.subGroup }">${ itemBean.subGroup }</option>
</c:if>
<option value="${ itemBean.name }">${ itemBean.name}</option>
</c:forEach>
Loop status:
${itemStatus.index} starts at 0
${itemStatus.count} starts at 1
You can find more information about loop status here.

jstl dropdown selected

So I have a spring binded dropdown similar to shown below. My question I want to
show ALL ID as the selected option whenever getAllID value is true. I know the selected
value is based on the value provided by spring bind element, in this case studentID but is there anyway I can override that anytime my boolean condition is true?
<spring:bind path="student.studentID">
<select name="${status.expression}" value="${status.value}">
<option />
<option value="001">1</option>
<option value="002">2</option>
<option value="003">3</option>
<option value="ALLID"
<c:if test='${student.getAllID eq true}'> Selected </c:if>>ALL ID
</option>
</select>
</spring:bind>
I think I got the fix. Added c:if to Select tag:
<c:if test='${student.getAllID ne true}'>
value="${status.value}"
</c:if>

jsp spring tag library issue

I have the following mark-up in my jsp:
<form:select id="ddlSkillLevelCoreFrom1" path="aarKpis" multiple="false" class="notSelectable skillsFrom qar_dd war_skill5 validate[required]">
<option value="">Please select a Skill Level From</option>
<c:forEach var="skillLevel" items="${skillLevels}">
<c:if test="${selectedSoftSkill.skillLevelId == skillLevel.skillLevelId}">
<option selected="selected" value="${skillLevel.skillLevelId}">
<c:set scope="request" var="skillLevelFromSelected">${skillLevel.skillLevelId}</c:set>
<c:out value="${skillLevel.name}" />
</option>
</c:if>
<c:if test="${selectedSoftSkill.skillLevelId != skillLevel.skillLevelId}">
<option value="${skillLevel.skillLevelId}">
<c:out value="${skillLevel.name}" />
</option>
</c:if>
</c:forEach>
</form:select>
<form:select id="ddlSkillLevelCoreTo1" path="aarKpis" multiple="false" class="notSelectable skillsTo qar_dd war_skill5 validate[required]">
<option value="">Please select a Skill Level To</option>
<c:forEach var="skillLevel" items="${skillLevels}">
<c:if test="${skillLevel > skillLevelFromSelected}">
<option value="${skillLevel.skillLevelId}">
<c:out value="${skillLevel.name}" />
</option>
</c:if>
</c:forEach>
</form:select>
as you can see in the first form:select based on the previously selected value i set the same value in a variable using c:set. Then I want to use that value to filter and show all elements with bigger Ids than that value in the second form:select. For some odd reason, it shows all elements unfiltered in the second form:select. Can you detect what I am missing.
Thanks for taking the time to answer my question.
here: <c:if test="${skillLevel > skillLevelFromSelected}">
SkillLevelFromSelected is an id, so shouldn't you take skillLevel.skillLevelId instead of skillLevel only?

Resources