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

<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!

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>

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.

JSTL c:if Comparison Failing

I am using the following block of code, which is meant to conditionally display a Spring MVC form tag checkbox:
"<c:out value="${contractForm.option}" />"
<c:if test="${ (contractForm.option == ' 4') || (contractForm.option == ' 5')} ">
<form:checkbox path="hold" /><form:label path="hold">Hold</form:label>
</c:if>
The "option" field is a 2-character fixed-width field. The <c:out> will print " 5", but the <c:if> fails. Can anyone help?
Jason
For some reason, <c:choose> with <c:when> blocks worked. Thanks to JB Nizet for tryng to assist.

How to split number in a jstl variable

I have this value
<c:set var="string1" value="SS4444"/>
What I am try to achieve is to get the numbers only (4444) from the above variable. I can use substring, but the index value might change, ie it can be 'SS4444', 'S4444', 'SSS444', so cant rely on static index.
Thanks in advance
Split it with regex.. (substringAfter is not proper for this..):
<c:forEach var="string1" items="${fn:split(yourstring, '^\w*')}">
<c:set var="string2" value="${string1}"/>
</c:forEach>
You can try with replace function as:
<c:set var="strng" value="SS4444"/>
<c:set var="strng1" value="${fn:replace(strng,'S','')}"/>

Resources