jstl forToken with Nested HashMap in request Attribute

*爱你&永不变心* 提交于 2019-12-11 09:24:08

问题


I have a HashMap (hshFields) of HashMaps (ecd_date, owned_by, etc..) with keys (label, size, etc..that I access as such:

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

(note: I must use JSTL and not EL)

the about spits out the a "label" of the field (maintained in an XML map) i.e.:

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

I'd like to now use a jstl forToken to loop over the nested HashMap. But I can not get it to work. Here is one of my attempts:

 <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>

Is this not working because Of incorrect syntax or hopefully not because I don't have EL capability??

EDIT OK based on skaffman's response below I have:

<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>

and the output is:

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

It seems to be working only on the first token because if I use the following:

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>

I get this output:

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

回答1:


Your syntax isn't quite right, it should be

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

rather than

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

Nested EL expressions like that isn't permitted.

updated: The reason it's only working for the first iteration in the loop is because you have spaces as well as commas in your items list, and the delims only handles the commas. So change the loop to

items="commitment_id,owned_by,fndsource"

rather than

items="commitment_id, owned_by, fndsource"

Otherwise, the spaces will form part of the individual loop values.



来源:https://stackoverflow.com/questions/3603067/jstl-fortoken-with-nested-hashmap-in-request-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!