title: Spring + Tiles2 + Freemarker - integrate via Freemarker Servlet or via Spring's FreeMarkerViewResolver?
body: <p>I was strugling today trying to migrate from Freemarker to Tiles2 + Freemarker.</p>
  
  <p>My freemarker templates use macros that come from spring.ftl. </p>
  
  <p>If I provide a fremarker servlet in web.xml, my model is visible to freemarker, but specific spring variables (naturally) are not populated into the model as springs FreemarkerView is responsible for that.</p>
  
  <p>If I configure a separate DispatcherServlet for specific url (say "/tpl/*") and configure freemarker resolver as default view resolver for that servlet and provide UrlFilenameViewController as default controller, special spring variables do get populated to model, but my own model is not visible: it is bound as a request attribute. I can access my model via ${Request.mymodel.myvar} but this way I have to change all my freemarker templates and I see something smelly in the idea.</p>
  
  <p>Now my solution was to extend UrlFilenameViewController and add my model from request to ModelAndView:</p>
  
  <pre><code>protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)  {
          ModelAndView mav = super.handleRequestInternal(request, response);
  
          HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
  
          Enumeration&lt;String&gt; attributes = request.getAttributeNames();
  
          while(attributes.hasMoreElements()) {
              String attribute = attributes.nextElement();
  
              if("model".equals(attribute)) {
                  logger.debug("FreemarkerViewController.handleRequestInternal: putting attribute to model: " + attribute + "=" + request.getAttribute(attribute));
                  map.put(attribute, request.getAttribute(attribute));
              }
          }
          logger.debug("FreemarkerViewController.handleRequestInternal: VIEW: " + mav.getViewName());
          return new ModelAndView(mav.getViewName(), map);
      }
  </code></pre>
  
  <p>But this solution is somewhat smelly too - if I add something to the model in my business controllers, I have to add it here.</p>
  
  <p>Is there an elegant solution for my problem?</p>