title: Spring MVC, Tiles, Multiple Controllers
body: <p>I'm experimenting with Spring MVC for an upcoming project. I'll need some web pages in the application to render multiple reusable "components" and figured that Tiles should help me accomplish this? Each component would have it's own controller. </p>
  
  <p>The example I've put together is only partially working. It's a page with 3 tiles. The 3rd tile has a controller that's trying to return an ArrayList (via @ModelAttribute annotation) to the client but the ArrayList is empty when the view is included as a tile.</p>
  
  <p>Here's the tiles setup in *-servlet.xml:</p>
  
  <pre><code>&lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/&gt;
          &lt;property name="order" value="0"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"&gt;
          &lt;property name="definitions"&gt;
              &lt;list&gt;
                  &lt;value&gt;/WEB-INF/tiles-config/tiles.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="viewResolver2" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="order" value="1" /&gt;
          &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Here's my tiles config (tiles.xml):</p>
  
  <pre><code>&lt;tiles-definitions&gt;
      &lt;definition name="parent" template="/WEB-INF/jsp/tiles/parent_layout.jsp"&gt;
          &lt;put-attribute name="header" value="/WEB-INF/jsp/tiles/header.jsp"/&gt;
          &lt;put-attribute name="body" value=""/&gt;
          &lt;put-attribute name="recordsSection" value=""/&gt;
          &lt;put-attribute name="widgetsSection" value=""/&gt;
      &lt;/definition&gt;
          &lt;definition name="_n/list.htm" extends="parent"&gt;
              &lt;put-attribute name="recordsSection" value="/WEB-INF/jsp/_n/list.jsp"/&gt;
              &lt;put-attribute name="widgetsSection" value="/WEB-INF/jsp/_n/widgets.jsp"/&gt;
          &lt;/definition&gt;
  &lt;/tiles-definitions&gt;
  </code></pre>
  
  <p>Here's the JSP code behind the tile in question. The "widgetsList" expression is what is bound to the controller method but coming back empty when this view is included as a tile.</p>
  
  <pre><code>&lt;%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false"%&gt;
  &lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;    
  &lt;%@taglib prefix="spring" uri="http://www.springframework.org/tags"%&gt;
  &lt;%@taglib prefix="display" uri="http://displaytag.sf.net" %&gt;
  
  &lt;h3&gt;Widgets&lt;/h3&gt;
  &lt;p&gt;widgetsList=${widgetsList}&lt;/p&gt;
  &lt;div id="displaylist"&gt;
      &lt;display:table id="items" name="${widgetsList}" cellspacing="0" export="true"&gt;
          &lt;display:column title="Widget ID" property="widgetId"/&gt;
          &lt;display:column title="Widget Name" property="widgetName"/&gt;
          &lt;display:column title="Widget State" property="widgetState"/&gt;
      &lt;/display:table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here's the controller:</p>
  
  <pre><code>@Controller
  public class WidgetController {
  
      @Autowired
      private WidgetDaoImpl widgetDao = new WidgetDaoImpl();
  
      @RequestMapping("_n/widgets.htm")
      public String widgets() {
          return "_n/widgets.htm";
      }
  
      @ModelAttribute("widgetsList")
      public ArrayList&lt;Widget&gt; getWidgets() {
          System.out.println("executing getWidgets()");
          ArrayList&lt;Widget&gt; records = widgetDao.listWidgets();
          return records;
      }
  }
  </code></pre>
  
  <p>When I access the the view "widgets.jsp" directly, the "widgetsList" attribute gets populated, it's only when it's added as a tile that it returns nothing.</p>