JSTL - if condition [duplicate] - jstl

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>

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.

Java Server page

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.

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!

How can i check if an attribute is set (not null and not an empty string) with jstl? [duplicate]

This question already has answers here:
Evaluate empty or null JSTL c tags
(8 answers)
Closed 6 years ago.
E.g.
<c:if test="${post}">
<h3>${post.title}</h3>
</c:if>
Use the empty keyword
<c:if test="${not empty post}">
<h3>${post.title}</h3>
</c:if>
You can also use '!' instead 'not' :
<c:if test="${!empty post}">
<h3>${post.title}</h3>
</c:if>

Resources