我有一个由Integer键入的Map.使用EL,我如何通过其键访问值?
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
Run Code Online (Sandbox Code Playgroud)
我认为这会有效,但它没有(地图已经在请求的属性中):
<c:out value="${map[1]}"/>
Run Code Online (Sandbox Code Playgroud)
跟进:我追查了问题.显然${name[1]},使用数字进行地图查找Long.我想通了这一点,当我换HashMap到TreeMap和收到的错误:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Run Code Online (Sandbox Code Playgroud)
如果我将地图更改为:
Map<Long, String> map = new HashMap<Long, String>();
map.put(1L, "One");
Run Code Online (Sandbox Code Playgroud)
然后${name[1]}返回"一".那是什么?为什么<c:out>将数字视为一个长数.对我来说似乎违反直觉(因为int比长期更常用).
所以我的新问题是,是否有EL符号通过Integer值访问地图?
Von*_*onC 114
初步答复(EL 2.1,2009年5月)
正如在这个java论坛帖子中提到的:
基本上,autoboxing将Integer对象放入Map中.即:
map.put(new Integer(0), "myValue")
Run Code Online (Sandbox Code Playgroud)
EL(表达式语言)将0评估为Long,因此寻找Long作为映射中的键.即它评估:
map.get(new Long(0))
Run Code Online (Sandbox Code Playgroud)
由于a Long永远不等于Integer对象,因此它不会在地图中找到该条目.
简而言之就是这样.
2009年12月,使用JSP 2.2/Java EE 6引入了EL 2.2,与EL 2.1相比有一些差异.
似乎(" EL表达式解析整数很长 "):
你可以在EL 2.2中调用对象self
intValue上的方法Long:
<c:out value="${map[(1).intValue()]}"/>
Run Code Online (Sandbox Code Playgroud)
这可能是一个很好的解决方法(下面也在Tobias Liefke的回答中提到)
原始答案:
EL使用以下包装器:
Terms Description Type
null null value. -
123 int value. java.lang.Long
123.00 real value. java.lang.Double
"string" ou 'string' string. java.lang.String
true or false boolean. java.lang.Boolean
Run Code Online (Sandbox Code Playgroud)
JSP页面演示了这个:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.*" %>
<h2> Server Info</h2>
Server info = <%= application.getServerInfo() %> <br>
Servlet engine version = <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %><br>
Java version = <%= System.getProperty("java.vm.version") %><br>
<%
Map map = new LinkedHashMap();
map.put("2", "String(2)");
map.put(new Integer(2), "Integer(2)");
map.put(new Long(2), "Long(2)");
map.put(42, "AutoBoxedNumber");
pageContext.setAttribute("myMap", map);
Integer lifeInteger = new Integer(42);
Long lifeLong = new Long(42);
%>
<h3>Looking up map in JSTL - integer vs long </h3>
This page demonstrates how JSTL maps interact with different types used for keys in a map.
Specifically the issue relates to autoboxing by java using map.put(1, "MyValue") and attempting to display it as ${myMap[1]}
The map "myMap" consists of four entries with different keys: A String, an Integer, a Long and an entry put there by AutoBoxing Java 5 feature.
<table border="1">
<tr><th>Key</th><th>value</th><th>Key Class</th></tr>
<c:forEach var="entry" items="${myMap}" varStatus="status">
<tr>
<td>${entry.key}</td>
<td>${entry.value}</td>
<td>${entry.key.class}</td>
</tr>
</c:forEach>
</table>
<h4> Accessing the map</h4>
Evaluating: ${"${myMap['2']}"} = <c:out value="${myMap['2']}"/><br>
Evaluating: ${"${myMap[2]}"} = <c:out value="${myMap[2]}"/><br>
Evaluating: ${"${myMap[42]}"} = <c:out value="${myMap[42]}"/><br>
<p>
As you can see, the EL Expression for the literal number retrieves the value against the java.lang.Long entry in the map.
Attempting to access the entry created by autoboxing fails because a Long is never equal to an Integer
<p>
lifeInteger = <%= lifeInteger %><br/>
lifeLong = <%= lifeLong %><br/>
lifeInteger.equals(lifeLong) : <%= lifeInteger.equals(lifeLong) %> <br>
Run Code Online (Sandbox Code Playgroud)
Dav*_*ave 11
除了上面的注释之外,另一个有用的提示是当某个变量(例如请求参数)中包含字符串值时.在这种情况下,传入此命令也会导致JSTL将say"1"的值键入为sting,因此在Map hashmap中找不到匹配项.
解决这个问题的一种方法是做这样的事情.
<c:set var="longKey" value="${param.selectedIndex + 0}"/>
Run Code Online (Sandbox Code Playgroud)
这将被视为一个Long对象,然后有机会匹配一个对象,当它包含地图Map或其他任何东西时.
然后像往常一样继续像往常一样
${map[longKey]}
Run Code Online (Sandbox Code Playgroud)
如果将数字放入"("")",则可以使用Long中的所有函数.这样你可以将long转换为int:
<c:out value="${map[(1).intValue()]}"/>
Run Code Online (Sandbox Code Playgroud)