JSP 中的 java.util.Map.contains() 方法调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6371249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:38:14  来源:igfitidea点击:

java.util.Map.contains() method call in JSP

javajspel

提问by user325643

Is there a way to call java.util.Map.contains() method in JSP where the Map is a property of a bean.

有没有办法在 JSP 中调用 java.util.Map.contains() 方法,其中 Map 是 bean 的一个属性。

回答by McDowell

${fooBean.fooMap.containsValue("baz")}

The above will work in JSP 2.2 or better. If you're using a pre-JSP 2.2 container (e.g. Java EE 5) then an EL function is probably the better solution.

以上将适用于 JSP 2.2 或更高版本。如果您使用的是 JSP 2.2 之前的容器(例如 Java EE 5),那么 EL 函数可能是更好的解决方案。

Static Java method:

静态Java方法:

package contains;
import java.util.Map;
public class Maps {
    public static boolean containsValue(Map<?, ?> map, Object value) {
        return map.containsValue(value);
    }
}

The file WEB-INF/tlds/maps.tld:

该文件WEB-INF/tlds/maps.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>maps</short-name>
  <uri>/WEB-INF/tlds/maps</uri>
   <function>
    <description>Returns true if the value is contained</description>
    <name>containsValue</name>
    <function-class>contains.Maps</function-class>
    <function-signature>
      boolean containsValue(java.util.Map, java.lang.Object)
    </function-signature>
  </function>
</taglib>

Usage:

用法:

<%@taglib prefix="maps" uri="/WEB-INF/tlds/maps" %>
...
${maps:containsValue(fooBean.fooMap, "baz")}

回答by BalusC

I'll assume that you're talking about using EL in JSP the right way and thus not the old fashioned scriptlets, otherwise the answer is extremely obvious like as given by AlexR.

我假设您正在谈论在 JSP 中以正确的方式使用 EL 而不是老式的scriptlets,否则答案非常明显,就像 AlexR 给出的那样。

You can use the emptykeyword to test for the presence of a non-null and non-empty value associated with a key.

您可以使用empty关键字来测试是否存在与键关联的非空和非空值。

<c:if test="${not empty bean.map['somekey']}">
    Map contains a non-null/non-empty value on key "somekey".
</c:if>

If you really need to invoke the map's containsKey()or containsValue()methods, then you need to ensure that you're running a Servlet 3.0 compatible container like Tomcat 7, Glassfish 3, JBoss AS 6, etc and that your web.xmlis declared conform Servlet 3.0. This way you can utilize a new EL 2.2 feature: invoking non-getter methods with arguments.

如果您确实需要调用映射containsKey()containsValue()方法,那么您需要确保您正在运行兼容 Servlet 3.0 的容器,如 Tomcat 7、Glassfish 3、JBoss AS 6 等,并且您web.xml的声明符合 Servlet 3.0。通过这种方式,您可以利用新的 EL 2.2 功能:使用参数调用非 getter 方法。

<c:if test="${bean.map.containsKey('somekey')}">
    Map contains key "somekey".
</c:if>
<c:if test="${bean.map.containsValue('somevalue')}">
    Map contains value "somevalue".
</c:if>

回答by cjstehno

You can use scriptlet code:

您可以使用脚本代码:

<%= yourBean.getMapProperty().contains() %>

It's not pretty, but it should work. There may also be some tag libraries available that do something similar.

它不漂亮,但它应该工作。也可能有一些标签库可以做类似的事情。

回答by AlexR

Since JSP is just a java you can call what you want. Just try it.

由于 JSP 只是一个 Java,因此您可以随意调用。去尝试一下。