Java 在 JSP/JSTL/EL 中访问集合的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3579548/
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
Access the size of a collection in JSP/JSTL/EL
提问by Drew Wills
I have a List variable called services
in my JSP page. I need to add some markup to the page if there's more than 1 element in the list.
services
我的 JSP 页面中有一个 List 变量。如果列表中的元素超过 1 个,我需要向页面添加一些标记。
What I'd like to do is...
我想做的是...
<c:if test="${services.size() gt 1}">
<!-- markup... -->
</c:if>
But you can't invoke methods on Java objects in EL (I think this is perhaps the 364823782 time I've regretted that fact). You can only access getterson Java objects by dropping the 'get,' e.g. ${user.name} for a User class that has a getName() method.
但是你不能在 EL 中调用 Java 对象上的方法(我想这可能是我后悔这个事实的 364823782 次)。您只能通过删除“get”来访问Java 对象上的getter,例如 ${user.name} 用于具有 getName() 方法的 User 类。
What's the right way to evaluate this test?
评估此测试的正确方法是什么?
采纳答案by Thorbj?rn Ravn Andersen
You are looking for fn:length(services)
. Remember to define the fn namespace.
您正在寻找fn:length(services)
. 记得定义 fn 命名空间。
回答by Nickhil
Include the tag lib in jsp file
在jsp文件中包含标签lib
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Use
用
<c:if test="${fn:length(services) gt 1}">
<!-- markup... -->
</c:if>