Java Server page - jstl

I have implemented the if / else tag in JSTL like following. But its not working. If condition not checking.
<c:choose>
<core:if ${capital.nextCapital()} eq ${request.capital}>
<p> Yes. The capital of ${capital.nextState()} is ${capital.nextCapital()} </p>
</core:if>
<c:otherwise>
<p>No. The capital of ${capital.nextState()} is ${capital.nextCapital()} </p>
</c:otherwise>
</c:choose>

That is not valid syntax at all.
the standard prefix is c, not core
inside c:choose, you can use c:when and c:otherwise. Not c:if.
the boolean condition must be inside a test attribute:
Attributes must be surrounded by quotes:
<c:when test="...">
The whole boolean EL expression must be inside ${}:
<c:when test="${ ... }">
So the end result should be
<c:when test="${capital.nextCapital() eq request.capital}"> ... </c:when>
I suggest you re-read your book or tutorial about custom tags and the JSTL.

Related

JSTL <c:if> tag to compare two variables

Why the block never reaches "conditionTrue"? I have printed both the variables
authorEmail and scase.authorEmail and they are same string. If I use literal like 'abc#yahoo.com' to test the condition , it works but not if both the c:if params are variables.
<c:forEach var="scase" items="${section.subSectionList.get(0).SCaseList}">
<c:set var="authorEmail" >
<sec:authentication property="principal.email" />
</c:set>
<c:if test = "${authorEmail == scase.authorEmail}" >
<option>conditionTrue</option>
</c:if>
</c:forEach>
UPDATE - I got it to work by making these changes.
I commented out the entire c:set tag. Instead I added these line -
<sec:authentication var="principal" property="principal" />
My test condition was then :
<c:if test = "${principal.email == scase.authorEmail}" >
<option>conditionTrue</option>
</c:if>
And this worked. However, the reasoning behind it still baffles me. Any insight, much appreciated.

JSTL - if condition [duplicate]

This question already has answers here:
Evaluate empty or null JSTL c tags
(8 answers)
Closed 7 years ago.
How to use if in JSTL? I would like do something like this.
<c:if ${order.products} == NULL>
a
else
b
</c:if>
Try this:
<c:choose>
<c:when test="${order.products == null}">
a
</c:when>
<c:otherwise>
b
</c:otherwise>
</c:choose>
I believe the syntax is this or something similar.
<c:choose>
<c:when test="${order.products == null}">
a
</c:when>
<c:otherwise>
b
</c:otherwise>
</c:choose>

How do I concatenate a list of values in JSTL into a single JSTL var?

I’m using JBoss 7.1.3.Final and Spring 3.1.1.RELEASE. On my JSP page, how do I get all the ids of an array into a comma separated string? I have tried this:
<c:forEach var="subject" items="${category.subjects}" varStatus="status">
<c:if test="${status.index == 0}">
<c:set var="cateogrySubjects" value="${subject.id}"/>
</c:if>
<c:if test="${status.index > 0}">
<c:set var="categorySubjects" value="${subject.id},${categorySubjects}"/>
</c:if>
</c:forEach>
Unfortunately the last statement printed is always “subj1,subj1” in the array even if the intermediate values are “subj1”, “subj2”, “subj3”, etc. Any help on concatenating things would be great.

JSTL - How to use a possessive apostrophe in a string?

<c:choose>
<c:when test="${place=='Bob's Car'}">
<c:set var="place" value="Truck"/>
</c:when>
</c:choose>
Code is not working because of apostrophe from " Bob's ". Is there any solution?
Try use \ this make java understand a simple apostrophe in your expression language.
For me run!

Issue with empty space value in countries list

I am currently working on a Spring project and I faced an issue with countries list.
The following list is a list of countries, I need to have a country with name emptySpace and id 0. the list is displaying countries successfully but this one country with empty space value is displayed by its id instead of its value, and if I full the empty space with any value, like "good job" it will show good job in the country list.
<c:forEach var="country" items="${country}">
<c:choose>
<c:when test="${country.entityId == companyDetails.countryId}">
<form:option value="${country.entityId}" selected="selected">${country.name}</form:option>
</c:when>
<c:otherwise>
<form:option value="${country.entityId}">${country.name}</form:option>
</c:otherwise>
</c:choose>
</c:forEach>
any suggestions?
You can try to use label attribute instead of tag body for passing label value:
<form:option value="${country.entityId}" label = "${country.name}" />
Since you are not using any feature of the spring form:option tag, try using the html <option> tag instead. I will not do any automatic substitutions.

Resources