【问题标题】:jstl forToken with Nested HashMap in request Attribute请求属性中带有嵌套 HashMap 的 jstl forToken
【发布时间】:2010-08-30 18:18:57
【问题描述】:

我有一个带有键(标签、大小等)的 HashMap(ecd_date、owned_by 等)的 HashMap (hshFields),我可以这样访问:

<c:out value="${hshFields.ecd_date.label}" />
<c:out value="${hshFields.owned_by.label}" />
<c:out value="${hshFields.fnd_source.label}" />

(注意:我必须使用 JSTL 而不是 EL)

about 吐出字段的“标签”(维护在 XML 映射中),即:

commitment_id = Commitment Id 
owned_by = Commitement Owner
fndsource = Funding Source

我现在想使用 jstl forToken 来循环嵌套的 HashMap。但我无法让它工作。这是我的尝试之一:

 <c:forTokens items="commitment_id, owned_by, fndsource" delims="," var="curField">
    The Field Label is: <c:out value="${hshFields.${curField}.label}" /> <br />
    The Field Sixze is: <c:out value="${hshFields.${curField}.size}" /> <br />
</c:forTokens>

这不起作用是因为语法不正确还是希望不是因为我没有 EL 能力??

编辑 好的,根据下面 skaffman 的回复,我有:

<c:forTokens items="owned_by, ecd_date, commitment_id" delims="," var="curField">
  Label for <c:out value="${curField}" /> : <c:out value="${hshFields[curField].label}" /><br></br>
</c:forTokens>

输出是:

Label for owned_by : Commitment Owner
Label for ecd_date : 
Label for commitment_id : 

它似乎只适用于第一个令牌,因为如果我使用以下内容:

Label for owned_by : <c:out value="${hshFields.owned_by.label}" /> <br></br>
Label for ecd_date : <c:out value="${hshFields.ecd_date.label}" /> <br></br>
Label for commitment_id : <c:out value="${hshFields.commitment_id.label}" /> <br></br>

我得到这个输出:

Label for owned_by : Commitment Owner
Label for ecd_date : Estimated Completion Date
Label for commitment_id : Commitment Number

【问题讨论】:

  • 注意:您正在使用both JSTL EL。 JSTL 是那些&lt;c:xxx&gt; 标签,EL 是那些${} 的东西。
  • @BalusC 我的意思是我使用的是 jsp 1.2 所以这不起作用: 但这确实 " />
  • 这就是所谓的“模板文本中的EL”。在 JSP 2.0 之前它确实不起作用。

标签: java jsp jstl el


【解决方案1】:

你的语法不太对,应该是

<c:out value="${hshFields[curField].label}" />

而不是

<c:out value="${hshFields.${curField}.label}" />

这样的嵌套 EL 表达式是不允许的。

更新:它仅适用于循环中的第一次迭代的原因是因为您的items 列表中有空格和逗号,而delims 只处理逗号。所以把循环改成

items="commitment_id,owned_by,fndsource"

而不是

items="commitment_id, owned_by, fndsource"

否则,空格将构成各个循环值的一部分。

【讨论】:

  • 谢谢。但是,我进行了更改,现在我只获得了我的第一个令牌的标签。后面的人吐出一片空白。我在上面编辑了我的帖子。
猜你喜欢
  • 2013-08-23
  • 2020-08-04
  • 1970-01-01
  • 2018-11-09
  • 2017-05-28
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多