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. Is there a way to access the value of a Ma开发者_运维知识库p in a JSP given the key?
// 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. Is this possible?
Thanks!
Did you try this :
<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,
<s:property value="%{headerMap.INITIATED}" />
<s:property value="%{headerMap.REJECTED}" />
Try this
<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.
精彩评论