JSTL c:if Comparison Failing - jstl

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.

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!

JSTL: How to print a newline

How can I print a newline ("\n" or "\r\n" or "\n\r"). Which is the right one to be understood by a browser?) using JSTL or EL? I want to really print a newline (not a <BR>), since I need to place it in a javascript section in a HTML file.
Try the xml entities for this:
for a newline and 
 for carriage return.
Simple, solution is just not to use JSTL/EL
<% out.print("\n"); %>
Even simpler:
<%= '\n' %>
You are asking the wrong question in your main post, and you later added it in a comment (perhaps you should edit your post to reflect the information in the comment?):
I need to put a \n after a // <![CDATA[ to end the comemnt before actaul JS code starts.
The easiest way to fix your issue is to comment out the CDATA using a block comment like this:
/* <![CDATA[ */
This will allow you to continue your code on the same line and it will not be part of the comment.
Example
/* <![CDATA[ */ var foo = "var";alert( foo ); /* ]]> */
Try the below concept and it works.
<c:set var="String1" value="line1 line2 line3 line4" />
<c:set var="String2" value="${fn:split(String1, ' ')}" />
<c:set var= "new" value="<br />" />
<c:out value="${String2[0]}${new}" escapeXml="false" />
<c:out value="${String2[1]}${new}" escapeXml="false" />
<c:out value="${String2[2]}${new}" escapeXml="false" />
<c:out value="${String2[3]}${new}" escapeXml="false" />

Resources