Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set boolean value into variable using JSTL tags?

Tags:

java

jsp

jstl

I am using JSTL tags. i have below code.

<c:set var="refreshSent" value="false"/> 

Now variable refreshSent has boolean value or String?

Thanks!

like image 568
user1016403 Avatar asked Sep 20 '12 12:09

user1016403


2 Answers

It is going to be a boolean. You can check it by comparing in a

<c:if test="${refreshSent eq false}"> 

and

<c:if test="${refreshSent eq 'false'}"> 

The second is a string comparison.

like image 138
Chris Avatar answered Sep 22 '22 03:09

Chris


It is a String.

The following JSP code:

<c:set var="refreshSent" value="false" /> <c:set var="refreshSent2" value="${false}" /> <c:set var="refreshSent3" value="${'false'}" />  <div> <div>${refreshSent} : ${refreshSent['class']}</div> <div>${refreshSent2} : ${refreshSent2['class']}</div> <div>${refreshSent3} : ${refreshSent3['class']}</div> </div> 

Outputs the following in a browser:

false : class java.lang.String false : class java.lang.Boolean false : class java.lang.String 

But if you use the refreshSent variable in an EL expression where a boolean is expected, it will be converted to a boolean by a call to Boolean.valueOf(String) (according to the JSP Specification).

So if you use:

<c:if test="${refreshSent}"> 

the value of the test attribute will be set to a boolean false value. The ${refreshSent} expression results in a String, but since the test attribute expects a boolean, a call to Boolean.valueOf("false") is made.

like image 37
John S Avatar answered Sep 23 '22 03:09

John S