Setting First Option in <Select> - spring

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>

Related

How can we get random option value from the dropdown list in jmeter?

What the below code like:
<td style="width:150px;">
<select id="ddlMgrMpng" onchange="changeMgr('140')" class="form-control input-sm">
<option value="first" selected="selected">
First Me
</option>
<option value="second">
Second Me
</option>
</select>
</td>
How can I capture values "First Me" and "Second Me" in jmeter?
Check the below CSS extractor:-
Use the same in "CSS/JQuery Extractor".
Hope this helps.

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.

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

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

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