【问题标题】:Accessing an element via variable alias in jsp通过jsp中的变量别名访问元素
【发布时间】:2015-08-13 17:37:53
【问题描述】:

我已经在这个问题上挂了一段时间了,这让我发疯了。

我在 java 中有一个看起来像这样的哈希:

hash.get("1") => "Hello"
hash.get("2") => "Hello"
hash.get("3") => "Hello"
hash.get("4") => "Hello"

这在 JSP 中可以得到“1”:

${hash["1"]}

但这不是:

<c:forEach begin='1' end='4' var='index'>
    ${index}
    ${hash[index]}
</c:forEach>

为什么我不能通过变量的别名访问 JSP 中散列中的元素?我可以在哈希上使用 foreach 循环,但我不想这样做。是否有可能只通过变量别名键访问哈希元素?

【问题讨论】:

  • ${hash[index + '']} 应该可以工作。 index 是一个数字,而不是一个字符串。
  • 为什么你有一个Map&lt;String, V&gt;而不是Map&lt;Long, V&gt;

标签: jsp hashmap jstl el


【解决方案1】:

知道了:

 <c:set var="stringId">${index}</c:set>
 ${ratingsCount[stringId]}

来源:Convert integer value to string using JSTL/EL

【讨论】:

  • 以我们的 cc-by-sa 许可证的名义,如果您引用/链接找到此答案的 source 将是公平的。
  • 抱歉,链接来源。
猜你喜欢
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2020-11-10
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多