java JSP EL 中 LENGTH[...] 和 fn:length(...) 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4134655/
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
Difference between LENGTH[...] and fn:length(...) in JSP EL
提问by Sammy
What is the difference between the LENGTH[...]
and the JSTL function fn:length(...)
?
LENGTH[...]
和 JSTL 函数有fn:length(...)
什么区别?
I tried to search to difference but I did not see any example that uses the first one.
我试图寻找差异,但我没有看到任何使用第一个的例子。
Here is an example:
下面是一个例子:
<c:when test="${object.field ne null || LENGTH[object.field] > 0}">
<td valign="top">
.....print something
</td>
</c:when>
回答by BalusC
Since there is no such function like LENGTH[...]
in standard JSP/JSTL/EL, it's impossible to tell about the differences. The fn:length()
is the only way to obtain the length of a String
, an Object[]
or Collection
.
由于没有像LENGTH[...]
标准 JSP/JSTL/EL 中那样的功能,因此无法说明差异。这fn:length()
是获取 a String
、 anObject[]
或长度的唯一方法Collection
。
${fn:length(someCollection)}
Updateas per your (fixed) example:
根据您的(固定)示例更新:
<c:when test="${object.field ne null || LENGTH[object.field] > 0}">
I've never seen this before. It look like that your webapp/servletcontainer is using a custom EL resolver. If this is true, you should see it been declared in webapp's web.xml
file.
我以前从未见过这个。看起来您的 webapp/servletcontainer 正在使用自定义 EL 解析器。如果这是真的,您应该会看到它已在 webapp 的web.xml
文件中声明。
Regardless, you'd rather like to use the EL empty
keyword here. It not only checks for null
, but also for the length of the String
, Object[]
or Collection
.
无论如何,您更愿意在empty
此处使用 EL关键字。它不仅检查null
, 还检查String
, Object[]
or的长度Collection
。
<c:when test="${not empty object.field}">
No need for fn:length()
here.
fn:length()
这里不需要。
The brace notation []
is in turn by the way often used to access properties by dynamic keys. E.g.
大括号表示法[]
通常用于通过动态键访问属性。例如
${bean[propertyname]}
If propertyname
resolves to "foo", then the above does effectively the same as ${bean.foo}
. It's also often used on Map
objects in the scope.
如果propertyname
解析为“foo”,则上述内容与${bean.foo}
. 它也经常用于Map
范围内的对象。