I've got a tag file, "nav.tag" with the following attribute:

<%@ attribute name="navigation" rtexprvalue="true" type="java.util.Map" %>

I get nothing when I attempt to assign a Map value to a variable like this:

<c:set var="articleSection" value="${navigation['ARTICLE']}" />

However, this works:

<c:forEach items="${navigation}" var="s">
  <c:choose>
    <c:when test="${s.key == 'ARTICLE'}">
       <c:set var="articleSection" value="${s.value}"/>
    </c:when>
  </c:choose>
</c:forEach>

Looping through the map keys this way is cumbersome. According to the docs the bracket notation should work. Is there something I'm missing?

Thanks!
James

I figured out the issue.

The Map that I was iterating over was keyed by an Enum. Here's the doEndTag method of the Tag class.

public int doEndTag() throws JspException {
    pageContext.setAttribute(var, getNavigationService().findAllSections());
    return super.doEndTag();
  }

The return type for getNavigationService().findAllSections() is

// ContentType is an enum
Map<ContentType, List<Section>>

I created a private transformKeysToStrings method in the tag class (is there a more efficient way to do this transformation?)

private Map<String, List<Section>> transformKeysToStrings(Map<ContentType, List<Section>> inputMap) {
    Map<String, List<Section>> outputMap = new HashMap<String,List<Section>>();
    for (Object key : inputMap.keySet()) {
      outputMap.put(key.toString(), inputMap.get(key));
    }
    return outputMap;
  }

and added it to my doEndTag method, which now looks like this

public int doEndTag() throws JspException {
    pageContext.setAttribute(var, transformKeysToStrings(getNavigationService().findAllSections()));
    return super.doEndTag();
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.