Java 评估 JSTL 中的 list.contains 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1490139/
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
Evaluate list.contains string in JSTL
提问by OscarRyz
I need to hide an element if certain values are present in the JSP
如果 JSP 中存在某些值,我需要隐藏一个元素
The values are stored in a List so I tried:
这些值存储在一个列表中,所以我试过:
<c:if test="${ mylist.contains( myValue ) }">style='display:none;'</c:if>
But, it doesn't work.
但是,它不起作用。
How can I evaluate if a list contains a value in JSTL, the list and the values are strings.
如何评估列表是否包含 JSTL 中的值,列表和值是否为字符串。
采纳答案by Kaleb Brasee
Sadly, I think that JSTL doesn't support anything but an iteration through all elements to figure this out. In the past, I've used the forEach method in the core tag library:
可悲的是,我认为 JSTL 只支持迭代所有元素来解决这个问题。过去,我使用过核心标签库中的 forEach 方法:
<c:set var="contains" value="false" />
<c:forEach var="item" items="${myList}">
<c:if test="${item eq myValue}">
<c:set var="contains" value="true" />
</c:if>
</c:forEach>
After this runs, ${contains} will be equal to "true" if myList contained myValue.
运行后,如果 myList 包含 myValue,则 ${contains} 将等于“true”。
回答by Chii
there is no built-in feature to check that - what you would do is write your own tld function which takes a list and an item, and calls the list's contains() method. e.g.
没有内置功能可以检查 - 您要做的是编写自己的 tld 函数,该函数接受一个列表和一个项目,并调用列表的 contains() 方法。例如
//in your own WEB-INF/custom-functions.tld file add this
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0"
>
<tlib-version>1.0</tlib-version>
<function>
<name>contains</name>
<function-class>com.Yourclass</function-class>
<function-signature>boolean contains(java.util.List,java.lang.Object)
</function-signature>
</function>
</taglib>
Then create a class called Yourclass, and add a static method called contains with the above signature. I m sure the implementation of that method is pretty self explanatory:
然后创建一个名为 Yourclass 的类,并添加一个名为 contains 的静态方法,并具有上述签名。我确信该方法的实现是不言自明的:
package com; // just to illustrate how to represent the package in the tld
public class Yourclass {
public static boolean contains(List list, Object o) {
return list.contains(o);
}
}
Then you can use it in your jsp:
然后你可以在你的jsp中使用它:
<%@ taglib uri="/WEB-INF/custom-functions.tld" prefix="fn" %>
<c:if test="${ fn:contains( mylist, myValue ) }">style='display:none;'</c:if>
The tag can be used from any JSP in the site.
可以从站点中的任何 JSP 使用该标记。
edit: more info regarding the tld file - more info here
编辑:有关 tld 文件的更多信息 - 此处有更多信息
回答by svachon
The following is more of a workaround than an answer to your question but it may be what you are looking for.
If you can put your values in a map instead of a list, that would solve your problem. Just map your values to a non null value and do this <c:if test="${mymap.myValue ne null}">style='display:none;'</c:if>
or you can even map to style='display:none;
and simply output ${mymap.myValue}
以下更像是一种解决方法,而不是对您问题的回答,但它可能正是您正在寻找的。如果您可以将您的值放在地图而不是列表中,那将解决您的问题。只需将您的值映射到非空值并执行此操作,<c:if test="${mymap.myValue ne null}">style='display:none;'</c:if>
或者您甚至可以映射到style='display:none;
并简单地输出${mymap.myValue}
回答by tamersalama
Another way of doing this is using a Map (HashMap)
with Key, Value pairs representing your object.
另一种方法是使用Map (HashMap)
代表您的对象的键、值对。
Map<Long, Object> map = new HashMap<Long, Object>();
map.put(new Long(1), "one");
map.put(new Long(2), "two");
In JSTL
在 JSTL
<c:if test="${not empty map[1]}">
This should return true if the pair exist in the map
如果这对存在于地图中,这应该返回 true
回答by Vladislav
<c:if test="${fn:contains(task.subscribers, customer)}">
This works fine for me.
这对我来说很好用。
回答by xxg
If you are using Spring Framework, you can use Spring TagLib and SpEL:
如果您使用的是 Spring Framework,则可以使用 Spring TagLib 和 SpEL:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
---
<spring:eval var="containsValue" expression="mylist.contains(myValue)" />
<c:if test="${containsValue}">style='display:none;'</c:if>
回答by tk_
You need to use the fn:contains()
or fn:containsIgnoreCase()
function.
您需要使用fn:contains()
orfn:containsIgnoreCase()
函数。
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
...
...
<c:if test="${not fn:containsIgnoreCase(mylist, 'apple')}">
<p>Doesn't contain 'apple'</p>
</c:if>
or
或者
<c:if test="${not fn:contains(mylist, 'Apple')}">
<p>Contains 'Apple'</p>
</c:if>
Note:This will work like mylist.toString().contains("apple")
and if this is not what you are looking for better use a other approach.
注意:这会起作用mylist.toString().contains("apple")
,如果这不是您想要的更好的方法,请使用其他方法。
回答by Tony Hung
${fn:contains({1,2,4,8}, 2)}
OR
或者
<c:if test = "${fn:contains(theString, 'test')}">
<p>Found test string<p>
</c:if>
<c:if test = "${fn:contains(theString, 'TEST')}">
<p>Found TEST string<p>
</c:if>
回答by oOXAam
I found this solution amazing.
我发现这个解决方案很棒。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
ArrayList list = new ArrayList();
list.add("one");
list.add("two");
list.add("three");
%>
<c:set var="list" value="<%=list%>" />
<html>
<body>
My list is ${list}<br/>
<c:if test='${fn:contains(list, "two")}'>
My list contains two <br/>
</c:if>
<c:if test='${fn:contains(list, ",")}'>
My list contains ,
</c:if>
</body>
</html>
The output for the code above is
上面代码的输出是
My list is [one, two, three]
我的清单是[一、二、三]
My list contains two
我的清单包含两个
My list contains ,
我的清单包含,
I hope it helps someone.
我希望它可以帮助某人。
回答by Lucas Basquerotto
If you are using EL 3.0+, the best approach in this case is as this other answerexplained in another topic:
如果您使用的是 EL 3.0+,则在这种情况下的最佳方法是另一个主题中解释的其他答案:
For a
Collection
it's easy, just use theColleciton#contains()
method in EL.<h:panelGroup id="p1" rendered="#{bean.panels.contains('p1')}">...</h:panelGroup> <h:panelGroup id="p2" rendered="#{bean.panels.contains('p2')}">...</h:panelGroup> <h:panelGroup id="p3" rendered="#{bean.panels.contains('p3')}">...</h:panelGroup>
For an
Object[]
(array), you'd need a minimum of EL 3.0 and utilize its new Lambda support.<h:panelGroup id="p1" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p1').get()}">...</h:panelGroup> <h:panelGroup id="p2" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p2').get()}">...</h:panelGroup> <h:panelGroup id="p3" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p3').get()}">...</h:panelGroup>
If you're not on EL 3.0 yet, you'd need to create a custom EL function. [...]
对于一个
Collection
很简单,只需使用Colleciton#contains()
EL 中的方法。<h:panelGroup id="p1" rendered="#{bean.panels.contains('p1')}">...</h:panelGroup> <h:panelGroup id="p2" rendered="#{bean.panels.contains('p2')}">...</h:panelGroup> <h:panelGroup id="p3" rendered="#{bean.panels.contains('p3')}">...</h:panelGroup>
对于
Object[]
(数组),您至少需要 EL 3.0 并利用其新的 Lambda 支持。<h:panelGroup id="p1" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p1').get()}">...</h:panelGroup> <h:panelGroup id="p2" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p2').get()}">...</h:panelGroup> <h:panelGroup id="p3" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p3').get()}">...</h:panelGroup>
如果您尚未使用 EL 3.0,则需要创建自定义 EL 函数。[...]