JSTL <c:if> tag to compare two variables - jstl

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.

Related

How to compare two string in foreach loop of jsp/jstl?

I have the need to compare to strings in foreach loop.
My development environment:
1) MAC 10.11,
2) STS Version: 3.7.3.RELEASE
4) Spring Web MVC,
5) Pivotal tc Server Developer Edition v3.1
<c:set var="day" scope="session" value="${day}_${curId}"/>
<c:set var="new_day" scope="session" value="${2_456123}"/>
<c:if test="${day eq new_day}">
<p> Both are same.
</c:if>
But the system get hags at the line
<c:if test="${day eq new_day}">
Can somebody give any pointer ?
Thanks & Regards,
Arun Dhwaj
Here is demonstration code.
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:set var="day" scope="session" value="2"/>
<c:set var="curId" scope="session" value="456123"/>
<c:set var="day" scope="session" value="${day}_${curId}"/>
<c:set var="new_day" scope="session" value="2_456123"/>
<c:if test="${day eq new_day}">
Both are same.
</c:if>
that prints: Both are the same.
If I change line number 5 to
<c:set var="new_day" scope="session" value="${2_456123}"/>
which is what you posted, then I get an error message
contains invalid expression(s): javax.el.ELException: Failed to parse the expression [${2_456123}]
Most likely the problem is caused by the following assignment
<c:set var="day" scope="session" value="${day}_${curId}"/>
With the above line you are assigning to the var day a value that comes from another variable with the same name but not necessarily with the same scope. In other words you might have already a variable called day that lives in another scope.
The default scope is the page scope, then you have in order the request, session and application therefore if a variable with the name day is retrieved in the page or request scope that variable will be considered when you perform the test and the variable you have defined in the session scope will be ignored.
You have two options
Change the name of the variable defined in the session scope and use that name when performing the test
<c:set var="niceday" scope="session" value="${day}_${curId}"/>
<c:if test="${niceday eq ...}">
Specify the scope of the variable used in the c:if tag
<c:if test="${sessionScope.day eq ...}">
Hope that helped.

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 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','')}"/>

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 access a second array value with foreach index

i have two arrays (strings delimited with commas) and i made a foreach loop on one of this vars
i need to be able to access to the other string with the foreach index like
<c:set var="name" value="Zara,nuha,roshy" />
<c:set var="name2" value="Zara2,nuha2,roshy2" />
<c:forEach items="${name}" delims="," var="name" varstatus="i">
<c:out value="${name}"/><br>
</c:forEach>
i need to access name2 values, in the name foreach, is it possible without doing another foeach?
The varstatus variable you are using contains a value "index" that you can use.
But, you can't operate on a string like that (not that I know of at least).
First, you need to convert name2 to a proper array or list. Then you can access it inside the for loop:
${name2list[i.index]}
Now, how to convert it to an array? How about the split function?
<%#taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="name2list" value="${fn:split(name2, ',')}"/>
This can be done, contrary to some previous comments.
See the following example based on the question:
<c:set var="names" value="Zara,nuha,roshy" />
<c:set var="names2" value="Zara2,nuha2,roshy2" />
<c:forEach items="${names.split(',')}" varStatus="i" var="name" >
${name} : ${names2.split(',')[i.index]}<br/>
</c:forEach>
Essentially we're using a string split function with expression language to get a string array from list comma separated values. Within the loop we the get the second value from the names2 array by using the varStatus index. I believe this accomplishes the task.

Resources