Displaying data once using JSTL - 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.

Related

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>

xPath select finds several elements instead of one (sibling vs. following-sibling)

If I use the following xPath:
//*[contains(#class, 'some-label') and contains(., 'MT:Week')]
It gets me exactly one element (a <span>) as expected. So far so good.
What I want is to get the <select> right next to it, but by doing this:
//*[contains(#class, 'some-label') and contains(., 'MT:Week')]/following::select
It already selects two <select> elements. How do I select just one without using the name attribute (!) ?
The HTML:
<div>
<label>
<span class="some-label">MT:Month</span>
<select name="month" id="someID">
<option selected="selected" value="12">March 2015</option>
</select>
</label>
</div>
<div>
<label>
<span class="some-label">MT:Week</span>
<!-- want to select just the below one -->
<select class="width-50px" name="monthWeek" id="someOtherID">
<option selected="selected" value="">All</option>
<option value="CalendarWeek{year=2015, week=9}">9</option>
<option value="CalendarWeek{year=2015, week=10}">10</option>
</select>
</label>
</div>
<div>
<label>
<span class="some-label">MT:Type</span>
<select name="type" id="anotherID">
<option selected="selected" value="">All</option>
<option value="someValue">Value 1</option>
<option value="someValue2">Value 2</option>
</select>
</label>
</div>
You could use [1] to get the first:
//*[contains(#class, 'some-label') and contains(., 'MT:Week')]/following::select[1]
But you probably only want the one within the same label, so use following-sibling:
//*[contains(#class, 'some-label') and contains(., 'MT:Week')]/following-sibling::select

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