在 JSP 中通过变量访问 hashmap 值

发布于 2024-10-14 07:24:55 字数 451 浏览 3 评论 0原文

我有一个哈希图,它被放入请求中:

HashMap<Integer, String> myMap = ...
request.setAttribute("myMap", myMap);

在 JSP 中,我有一个 foreach 循环

<c:forEach items="${list}" var="item" varStatus="status">
   <c:out value="${item.description}"/>
   <c:out value="${myMap[item.id]}"/>
</c:forEach>

,但 ${myMap[item.id]} 不起作用。如何通过 item.id 变量访问 hashmap 的值?

I have a hashmap, which is put to the request:

HashMap<Integer, String> myMap = ...
request.setAttribute("myMap", myMap);

In JSP I have a foreach loop

<c:forEach items="${list}" var="item" varStatus="status">
   <c:out value="${item.description}"/>
   <c:out value="${myMap[item.id]}"/>
</c:forEach>

but ${myMap[item.id]} does not work. How can I access hashmap's value by item.id variable ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友欢 2024-10-21 07:24:55

在 EL 中,数字被视为Long。将您的 Map 更改为 Map 就可以了。

In EL, numbers are treated as Long. Change your Map to be a Map<Long, String> and it'll work.

流绪微梦 2024-10-21 07:24:55

我认为 beans 的 id 属性不是包装对象(Integer id;)。查看 Map

来自 JavaDoc 的文本

注意:如果出现以下情况,必须格外小心
可变对象用作映射键。
未指定映射的行为
如果对象的值改变
以影响平等的方式
以对象为关键的比较
在地图中。这方面的一个特例
禁止的是它不是
地图允许包含
本身作为钥匙。虽然它是
地图允许包含
本身作为一种价值,极度谨慎是
建议:equals 和 hashCode
方法不再明确定义
这样的地图。

Item.java

package com.me;

public class Item {
    private Integer id;
    private String description;

    public Item() {
    }

    public Item(Integer id, String description) {
        this.id = id;
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

}

JSP 片段

<%
HashMap<Integer, String> myMap = new HashMap<Integer, String>();
myMap.put(new Integer(1), "One");
myMap.put(new Integer(2), "Two");
myMap.put(new Integer(3), "Three");
request.setAttribute("myMap", myMap);

List<com.me.Item> list=new ArrayList<com.me.Item>();
list.add(new com.me.Item(1,"A - Desc"));
list.add(new com.me.Item(2,"B - Desc"));
list.add(new com.me.Item(3,"C - Desc"));
request.setAttribute("list", list);
%>

<c:forEach items="${list}" var="item" varStatus="status">
  <c:out value="${item.description}"/>
  <c:out value="${myMap[item.id]}"/>
</c:forEach>

I think the id attribute of beans is not a wrapper object (Integer id;). Have a look at doc page of Map.

Text from JavaDoc

Note: great care must be exercised if
mutable objects are used as map keys.
The behavior of a map is not specified
if the value of an object is changed
in a manner that affects equals
comparisons while the object is a key
in the map. A special case of this
prohibition is that it is not
permissible for a map to contain
itself as a key. While it is
permissible for a map to contain
itself as a value, extreme caution is
advised: the equals and hashCode
methods are no longer well defined on
a such a map.

Item.java

package com.me;

public class Item {
    private Integer id;
    private String description;

    public Item() {
    }

    public Item(Integer id, String description) {
        this.id = id;
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

}

JSP snippet

<%
HashMap<Integer, String> myMap = new HashMap<Integer, String>();
myMap.put(new Integer(1), "One");
myMap.put(new Integer(2), "Two");
myMap.put(new Integer(3), "Three");
request.setAttribute("myMap", myMap);

List<com.me.Item> list=new ArrayList<com.me.Item>();
list.add(new com.me.Item(1,"A - Desc"));
list.add(new com.me.Item(2,"B - Desc"));
list.add(new com.me.Item(3,"C - Desc"));
request.setAttribute("list", list);
%>

<c:forEach items="${list}" var="item" varStatus="status">
  <c:out value="${item.description}"/>
  <c:out value="${myMap[item.id]}"/>
</c:forEach>
梦亿 2024-10-21 07:24:55

您可以将键值放在 Java 端的映射中,并在 JSP 页面上使用 JSTL 访问相同的键值,如下所示:

优先java 1.7:

Map<String, String> map = new HashMap<String, String>();
map.put("key","value");

Java 1.7 及更高版本:

Map<String, String> map = new HashMap<>();
map.put("key","value");

JSP 片段:

<c:out value="${map['key']}"/>

You can put the key-value in a map on Java side and access the same using JSTL on JSP page as below:

Prior java 1.7:

Map<String, String> map = new HashMap<String, String>();
map.put("key","value");

Java 1.7 and above:

Map<String, String> map = new HashMap<>();
map.put("key","value");

JSP Snippet:

<c:out value="${map['key']}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文