日期:2014-05-16  浏览次数:20385 次

JSTL-map中key值为数字的处理方式

最好显示转化成Long型:

boardMap.put((Long)b.getId(), bclist);

如果写成boardMap.put(b.getId()+"", bclist);

页面上写成

?

<c:set var="boardKey" value="${board.id}"/>

 <c:forEach items="${boardMap[boardKey]}" var="boardItem">
   						<img id="img${boardItem.id}" src="<c:url value='images/node.gif'/>" alt="" align="absmiddle"/><a href="<c:out value='/forum/${board.id}'/>">${boardItem.boardName}</a>
   				</c:forEach>
?

如果boardKey等于1

循环中是不会有输出的,因为JSTL会将作为1处理,而不是"1".

参考http://stackoverflow.com/questions/924451/jstl-access-a-map-value-by-key

?

??? Basically autoboxing puts an Integer object into the Map. ie:

map.put(new Integer(0), "myValue")

EL (Expressions Languages) evaluates 0 as a Long and thus goes looking for a Long as the key in the map. ie it evaluates:

map.get(new Long(0))

As a Long is never equal to an Integer object, it does not find the entry in the map. Thats it in a nutshell.

EL uses the following wrappers:

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

JSP page demonstrating this:

?<%@ 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/>
??? li