I saw some code like the following in a JSP
<c:if test='<%=request.isUserInRole(RoleEnum.USER.getCode())%>'> <li>user</li> </c:if>
My confusion is over the ‘=’ that appears in the value of the test attribute. My understanding was that anything included within <%= %> is printed to the output, but surely the value assigned to test must be a Boolean, so why does this work?
For bonus points, is there any way to change the attribute value above such that it does not use scriptlet code? Presumably, that means using EL instead.
Cheers, Don
All that the
testattribute looks for to determine if something is true is the string ‘true’ (case in-sensitive). For example, the following code will print ‘Hello world!’The code within the
<%= %>returns a boolean, so it will either print the string ‘true’ or ‘false’, which is exactly what the<c:if>tag looks for.