Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL string comparison always returns false

Tags:

java

jsp

jstl

I am trying string comparison with

<c:if test="${dept eq 'account'}"></c:if>

But this always returns false. I check the dept variable had the value 'account'. I also tried like this

<c:if test="${dept == 'account'}"></c:if> 

This also returns false.

But if I use the java code like this then it works fine

<%
if(dept.equals("account")){

blah blah blah
}

%>

Any help would be really appreciated.

Thanks

like image 515
user509755 Avatar asked Nov 16 '10 16:11

user509755


1 Answers

The symptoms indicate that you've declared it in the scriptlet scope, not in the EL scope. Scriptlets and EL doesn't share the same scope. EL uses under the covers PageContext#findAttribute() to resolve the variable. Put dept in one of the page, request, session or application scopes. Which one to choose depends on the sole purpose of dept itself. I'd start with the request scope. E.g. in a servlet:

request.setAttribute("dept", dept);

This way it'll be available in EL by ${dept}.

After all, best is to avoid using scriptlets completely. Java code belongs in Java classes, not in JSP files.

like image 57
BalusC Avatar answered Oct 05 '22 21:10

BalusC