I'm not at Java developer but I'm trying to figure out how to get a value from a Map. I'm using a basic (old) Struts app with no fancy stuff or JSTL afaik. I can get all the key/value pairs to output by converting the Map to a String:
<% String myValue = pageContext.getSession().getAttribute("myMap").toString(); %>
However, when I try to access a specific key it doesn't work:
<% String myValue = pageContext.getSession().getAttribute("myMap['myKey']").toString(); %>
<% String myValue = ((Map) pageContext.getSession().getAttribute("myMap")).get("myKey").toString(); %>
resp.
<% String myValue = ((Map) session.getAttribute("myMap")).get("myKey").toString(); %>
// because session is an implicit object in JSP
However, I would strongly discourage you from using such spaghetti code in JSP. If you need to output the "myKey" value, you can use e.g. <c:out> tag with EL:
<c:out value="${session.myMap.myKey}" />
or
<c:out value="${session['myMap']['myKey']" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With