简体   繁体   English

如何在JSP中给出一个键的Map值?

[英]How to get Map value given a key in a JSP?

I have a Struts 2 application using the JSTL/Struts2/DisplayTag tag libraries in my JSP. 我的JSP中有一个使用JSTL / Struts2 / DisplayTag标记库的Struts 2应用程序。 Is there a way to access the value of a Map in a JSP given the key? 有没有办法在给定密钥的JSP中访问Map的值?

// Action code
Map<String,String> map = new HashMap<String,String>();

mapOnValueStack = map;

//add key/value pairs

fieldKeyOnValueStack = "1";//sets key

.... ....

<%-- JSP code --%>

<s:property value="%{mapOnValueStack.get(fieldKeyOnValueStack)}" />

Essentially I want to do map access within the JSP. 基本上我想在JSP中进行地图访问。 Is this possible? 这可能吗?

Thanks! 谢谢!

你试过这个:

<s:property value="%{mapOnValueStack.['fieldKeyOnValueStack']}" />

If You have used this in your action, 如果你在行动中使用了这个,

Map<String,Integer> headerMap=new HashMap<String, Integer>();
headerMap.put("INITIATED", 0);
headerMap.put("COMPLETED", 0);
headerMap.put("SUBMITTED", 0);
headerMap.put("APPROVED", 0);
headerMap.put("TRAFICKED", 0);
headerMap.put("REJECTED", 0);

Then use this on your jsp, 然后在你的jsp上使用它,

<s:property value="%{headerMap.INITIATED}" />
<s:property value="%{headerMap.REJECTED}" />

尝试这个

<s:property value="%{mapOnValueStack['fieldKeyOnValueStack']}" />

Try this 尝试这个

<c:forEach var="entry" items="${mapOnValueStack}">
    Name:  ${entry.key} <br/>
    Value: ${entry.value}
</c:forEach>

Hope it will work. 希望它会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM