Java JSTL <c:if> 标签中的测试属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/93408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
test attribute in JSTL <c:if> tag
提问by Dónal
I saw some code like the following in a JSP
我在 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?
我的困惑在于出现在test
属性值中的“=” 。我的理解是,其中包含的任何内容<%= %>
都会打印到输出中,但肯定分配给 test 的值必须是布尔值,那么为什么会这样呢?
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.
对于加分,有没有办法改变上面的属性值,使其不使用scriptlet代码?据推测,这意味着改用 EL。
Cheers, Don
干杯,唐
采纳答案by Michael
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!"
该test
属性为确定某事是否为真而查找的所有内容都是字符串“true”(不区分大小写)。例如,以下代码将打印“Hello world!”
<c:if test="true">Hello world!</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.
中的代码<%= %>
返回一个布尔值,因此它将打印字符串“true”或“false”,这正是<c:if>
标签要查找的内容。
回答by Sindri Traustason
Attributes in JSP tag libraries in general can be either static or resolved at request time. If they are resolved at request time the JSP will resolve their value at runtime and pass the output on to the tag. This means you can put pretty much any JSP code into the attribute and the tag will behave accordingly to what output that produces.
JSP 标记库中的属性通常可以是静态的,也可以在请求时解析。如果它们在请求时被解析,JSP 将在运行时解析它们的值并将输出传递给标记。这意味着您几乎可以将任何 JSP 代码放入该属性中,并且该标记将根据产生的输出进行相应的操作。
If you look at the jstl taglib docs you can see which attributes are reuest time and which are not. http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html
如果您查看 jstl taglib 文档,您可以看到哪些属性是重用时间,哪些不是。http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html
回答by Javaxpert
<%=%> by itself will be sent to the output, in the context of the JSTL it will be evaluated to a string
<%=%> 本身将被发送到输出,在 JSTL 的上下文中它将被评估为一个字符串
回答by Mike Spross
The expression between the <%= %> is evaluated before the c:if tag is evaluated. So, supposing that |request.isUserInRole| returns |true|, your example would be evaluated to this first:
<%= %> 之间的表达式在计算 c:if 标记之前计算。所以,假设 |request.isUserInRole| 返回 |true|,您的示例将首先评估为:
<c:if test="true">
<li>user</li>
</c:if>
and then the c:if tag would be executed.
然后 c:if 标签将被执行。
回答by Michael
You can also use something like
你也可以使用类似的东西
<c:if test="${ testObject.testPropert == "testValue" }">...</c:if>