86

整数でキー設定されたマップがあります。ELを使用して、キーで値にアクセスするにはどうすればよいですか?

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");

これは機能すると思いましたが、機能しません(マップがすでにリクエストの属性に含まれている場合):

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

フォローアップ:問題を追跡しました。どうやら${name[1]}、番号をとしてマップルックアップを実行しLongます。HashMapに変更しTreeMapてエラーを受け取ったときに、これを理解しました。

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

マップを次のように変更した場合:

Map<Long, String> map = new HashMap<Long, String>();
map.put(1L, "One");

その後、${name[1]}「One」を返します。それは何ですか?なぜ<c:out>数字を長いものとして扱うのですか。私には直感に反しているようです(intはlongよりも一般的に使用されているため)。

だから私の新しい質問は、Integer値によってマップにアクセスするためのEL表記法はありますか?

4

5 に答える 5

118

最初の回答 (EL 2.1、2009 年 5 月)

この Java フォーラムのスレッドで述べたように:

基本的に、オートボクシングは Integer オブジェクトを Map に入れます。すなわち:

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

EL (Expressions Languages) は 0 を Long として評価するため、マップ内のキーとして Long を探します。つまり、次のように評価されます。

map.get(new Long(0))

aLongがオブジェクトと等しくIntegerなることはないため、マップ内のエントリは見つかりません。
一言でいうと以上です。


2009 年 5 月以降の更新 (EL 2.2)

2009 年 12 月には、JSP 2.2/Java EE 6 を含む EL 2.2 が導入されましたが、 EL 2.1 とはいくつかの違いがあります。
(" EL Expression parsing integer as long ") ようです:

EL 2.2 内intValueのオブジェクト selfでメソッドを呼び出すことができますLong:

<c:out value="${map[(1).intValue()]}"/>

それはここで良い回避策になる可能性があります(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

これを示す 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>
于 2009-05-29T05:44:08.417 に答える
11

上記のコメントに加えて、もう1つの役立つヒントは、リクエストパラメータなどの変数に文字列値が含まれている場合です。この場合、これを渡すと、JSTLがたとえば「1」の値をスティングとしてキーイングするため、マップハッシュマップに一致するものが見つかりません。

これを回避する1つの方法は、このようなことを行うことです。

<c:set var="longKey" value="${param.selectedIndex + 0}"/>

これはLongオブジェクトとして扱われるようになり、マップMapなどに含まれているオブジェクトと一致する可能性があります。

次に、通常どおり次のようなものを続けます

${map[longKey]}
于 2011-03-29T14:44:03.567 に答える
10

"(" ")" に数値を入れると、Long のすべての関数を使用できます。そうすれば、long を int にキャストできます。

<c:out value="${map[(1).intValue()]}"/>
于 2014-12-17T19:34:06.057 に答える
3

変更できないキーを持っている場合は、カスタムEL関数Mapを記述してaをに変換できます。これにより、次のようなことができます。IntegerLongInteger

<c:out value="${map[myLib:longToInteger(1)]}"/>
于 2012-09-28T08:27:20.993 に答える
3

上記の投稿に基づいて、私はこれを試してみましたが、これはうまくいきました。マップ B の値をマップ A のキーとして使用したかったのです。

<c:if test="${not empty activityCodeMap and not empty activityDescMap}">
<c:forEach var="valueMap" items="${auditMap}">
<tr>
<td class="activity_white"><c:out value="${activityCodeMap[valueMap.value.activityCode]}"/></td>
<td class="activity_white"><c:out value="${activityDescMap[valueMap.value.activityDescCode]}"/></td>
<td class="activity_white">${valueMap.value.dateTime}</td>
</tr>
</c:forEach>
</c:if>
于 2012-01-24T00:53:31.290 に答える