Java – JSTL string comparison always returns false

I'm trying string comparison

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

But this always returns false I check that the value of the dept variable is "account" I've tried that, too

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

This also returns false

But if I use java code like this, it works

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

blah blah blah
}

%>

Any help would be appreciated

thank you

Solution

The symptom indicates that you have declared it within the scriptlet scope, not the El scope Scriptlets and El do not have the same scope El uses pagecontext #findattribute () to resolve variables Place dept in one of the page, request, session, or application scopes Which one to choose depends on the sole purpose of the Department itself I'll start with the scope of the request For example In a servlet:

request.setAttribute("dept",dept);

This can be obtained in El through ${dept}

After all, it's best to avoid using scriptlet completely Java code belongs to Java class, not in JSP file

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>