user509755
user509755

Reputation: 2981

JSTL string comparison always returns false

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

Upvotes: 10

Views: 14453

Answers (1)

BalusC
BalusC

Reputation: 1108702

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.

Upvotes: 14

Related Questions