Search code examples
javajspspring-mvcjstljsp-tags

Create and pass Map to a request scoped attribute


In my JSP model I have a bean of class MyBean in the scope.

class MyBean {
  ...
  public String getName() { ... }
  public String format(Map<String,String> model) { 
      /* format a template with model */ 
  }
}

I use it this way:

<b>${requestScope.myBean.name}</b> - ${requestScope.myBean.format(myMap)}

myMap would contain some information known only in the JSP page, e.g. URLs. Is there a tag/library that I can use to declare and pass a map e.g.

<lib:map var="myMap">
 <lib:mapEntry name="base_url" value="${some_url_value_in_scope}"/>
</lib:map>
<b>${requestScope.myBean.name}</b> - ${requestScope.myBean.format(myMap)}

Solution

  • If you are running EL 2.2 which is part of Servlet 3.0. Then ${requestScope.myBean.format(myMap) will work . Or else create a custom EL function and call it like ${someTag:format(requestScope.myBean, myMap)}

    There may be better way but you can do something like this :

    <jsp:useBean id="myMap" class="java.util.HashMap" scope="request"/>
    <c:set target="${myMap}" property="base_url" value="${some_url_value_in_scope}"/>