问题描述
我在JSP中看到了类似以下的代码
I saw some code like the following in a JSP
<c:if test="<%=request.isUserInRole(RoleEnum.USER.getCode())%>">
<li>user</li>
</c:if>
我的混淆超出了值中出现的=测试
属性。我的理解是,<%=%>
中包含的任何内容都会打印到输出中,但分配给test的值肯定必须是布尔值,那么为什么这样做呢? ?
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?
对于奖励积分,有没有办法更改上面的属性值,使其不使用scriptlet代码?据推测,这意味着使用EL代替。
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.
干杯,
Don
Cheers,Don
推荐答案
test
属性所有查找以确定某些内容是否为字符串true(不区分大小写)。例如,以下代码将打印Hello world!
All that the test
attribute looks for to determine if something is true is the string "true" (case in-sensitive). For example, the following code will print "Hello world!"
<c:if test="true">Hello world!</c:if>
<%=%> $ c内的代码$ c>返回一个布尔值,因此它将打印字符串true或false,这正是
< c:if>
标记所查找的内容。
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.
这篇关于JSTL中的测试属性< c:if>标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!