使用JSTL从Map获取值

时间:2013-12-26 04:05:10

标签: java json jsp map jstl

我试着在这里查看不同的问题,但没有一个完全匹配。

I get Map data from the source as <Key,Value> pairs, typically a JSON source

我尝试过这样的事情

<tr>
    <c:forEach items="${record.attributes.entry}" var="entry">
        <td style="width: 20%; height: 15px;">
            <img
                src="${entry['p_csl_thumbnail_url']}"
                class="img-responsive" />
        </td>
        <td style="width: 65%; height: 15px;" class="ellipses">
            ${entry['p_csl_description']}

        </td>
        <td style="width: 15%; height: 15px;" class="ellipses">
            ${entry['p_csl_close_time_est']}
        </td>
     </c:forEach>
</tr>

其中entry是Map,我试图使用密钥获取值。

示例数据:

{
  "records": [
    {
      "aggregationCount": "0",
      "attributes": {
        "entry": [
          {
            "key": "P_CSL_Description",
            "value": " [i]test[/i] Description"
          },
          {
            "key": "P_RSCG_Lot_Creation_Time",
            "value": "0"
          },
          {
            "key": "P_CSL_Close_Time_EST",
            "value": "2013-09-16 06:25:00 PM EST"
          },
          {
            "key": "P_CSL_Thumbnail_Url",
            "value": "http://localhost:9889/54205775_lists.jpg"
          }
        ]
      },
      "recordId": "LCOM6693760",
      "selfLocation": {
        "uri": "http://localhost:8899/search-engine/v1/collections/csl/search?recordId=LCOM6693760"
      }
    }
  ]
}

更新

<tr>
    <td style="width: 10%; height: 15px;">
        <img src="${record.attributes.entry['P_CSL_Thumbnail_Url']}" class="img-responsive" />
    </td>
    <td style="width: 80%; height: 15px;">
        ${record.attributes.entry['P_CSL_Description']}
    </td>
    <td style="width: 10%; height: 15px;">
        ${record.attributes.entry['P_CSL_Close_Time_EST']}
    </td>
</tr>

我收到以下错误

 SEVERE: Servlet.service() for servlet jsp threw exception
 java.lang.NumberFormatException: For input string:
 "P_CSL_Thumbnail_Url"  at
 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)     at
 java.lang.Integer.parseInt(Integer.java:527)   at
 javax.el.ListELResolver.coerce(ListELResolver.java:173)    at
 javax.el.ListELResolver.getValue(ListELResolver.java:52)   at
 org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183)    at
 org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at
 org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    at
 org.apache.jsp.view.jsp.search_jsp._jspx_meth_c_005fforEach_005f4(search_jsp.java:1225)

1 个答案:

答案 0 :(得分:2)

声明如

<c:forEach items="${record.attributes.entry}" var="entry">

entryMap.Entry类型的对象。你可以简单地做

    <td style="width: 20%; height: 15px;">
        <img
            src="${entry.key}"
            class="img-responsive" />
    </td>

获取密钥,${entry.value}获取值。