Nick
Nick

Reputation: 301

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

Upvotes: 4

Views: 52974

Answers (4)

Knowledge Serve
Knowledge Serve

Reputation: 616

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}" />

Upvotes: 5

Bourkadi
Bourkadi

Reputation: 746

Try this

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

Upvotes: 2

Raman
Raman

Reputation: 1527

Try this

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

Hope it will work.

Upvotes: -4

Michael Zilbermann
Michael Zilbermann

Reputation: 1428

Did you try this :

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

Upvotes: 7

Related Questions