2

I have a Model object ("foo") added to the ModelMap in the MVC controller:

Spring MVC Controller:

Foo foo = new Foo("FooName");
model.addAttribute("foo", foo);
return "foo";

I can call properties of the object in foo.jsp. I also set an alias for the foo object so that genericFoo can access the object - genericFoo expects the object to be called genericFoo.

foo.jsp:

<c:out value="${foo.name}"/> <!-- Displays "FooName" -->
<c:set var="genericFoo" value="${foo}"/>
<jsp:include page="genericFoo.jsp" />

However, genericFoo.jsp does not display properties of the object.

Why not?

genericFoo.jsp:

<c:out value="${genericFoo.name}"/> <!-- No value displayed displayed -->
Greg
  • 21
  • 1

1 Answers1

4

Because <jsp:include> does a dynamic include (it sort of dispatches the request to the included JSP). And <c:set> stores the object inside a page-scoped attribute. By definition, page-scoped attributes are only visible from the page which defined them.

Solutions:

  1. Use a static include: <%@include file="..."/> rather than a dynamic one
  2. put the attribute in a request scoped attribute: <c:set var="genericFoo" value="${foo}" scope="request"/>.
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255