title: Sitemesh and Freemarker with Jersey resources
body: <p>I have got a Jersey-based application, which for HTML requests uses Freemarker to generate HTML from templates programatically;</p>
  
  <pre><code>@GET
  @Produces("text/html")
  public Response getResources(@PathParam("name") String name) {
      LOG.info("got a request for " + name);
      Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();
      model.put("name", name);
      //lookup some data for name;
      model.put("somedata", somedata);
      StringWriter out = new StringWriter();
      configuration.getTemplate("template.ftl").process(model, out);
      return Response.ok().entity(out.toString()).build();
  }
  </code></pre>
  
  <p>So the configuration instance = freemarker.template.Configuration which is initialised in Spring;</p>
  
  <pre><code>&lt;bean id="configuration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"&gt;
      &lt;property name="templateLoaderPath" value="/WEB-INF/template/path"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Now, I've in web.xml the Jersey controller, the Sitemesh filter, and the Sitemesh-Freemarker servlet set up;</p>
  
  <pre><code>&lt;servlet&gt;
    &lt;servlet-name&gt;JerseySpringNode&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.sun.jersey.spi.spring.container.servlet.SpringServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;JerseySpringNode&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/mypath/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>The sitemesh filter:</p>
  
  <pre><code>&lt;filter&gt;
    &lt;filter-name&gt;sitemesh&lt;/filter-name&gt;
    &lt;filter-class&gt;com.opensymphony.sitemesh.webapp.SiteMeshFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
    &lt;filter-name&gt;sitemesh&lt;/filter-name&gt;
    &lt;url-pattern&gt;/mypath/*&lt;/url-pattern&gt;
    &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
    &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>And the servlet:</p>
  
  <pre><code>&lt;servlet&gt;
    &lt;servlet-name&gt;sitemesh-freemarker&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet&lt;/servlet-class&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;TemplatePath&lt;/param-name&gt;
      &lt;param-value&gt;/&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;default_encoding&lt;/param-name&gt;
      &lt;param-value&gt;ISO-8859-1&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;load-on-startup&gt;3&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;sitemesh-freemarker&lt;/servlet-name&gt;
    &lt;url-pattern&gt;*.dec&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>(we use ".dec" to mark templates that are decorators). Decorator.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;decorators defaultdir="/decorators"&gt;
      &lt;decorator name="main" page="header_and_footer.dec"&gt;
        &lt;pattern&gt;/*&lt;/pattern&gt;
      &lt;/decorator&gt;
  &lt;/decorators&gt;
  </code></pre>
  
  <p>Now, this works fine as it is. HTML pages are decorated with sitemesh.</p>
  
  <p>The only problem that we currently have, is that we now need to inject into the decorators parameters which are determined from Spring (such as things from the user principle, things out of the request context, or stuff from the database, e.g. some information from that "somedata" property). </p>
  
  <p>Given we are not using Spring WebMVC anywhere in this application, how do we wire up a freemarker servlet that can access this information? Even if it's a separate Spring bean that requires to be wired up with its dependencies, that's OK. Alternatively is there some way we can programatically invoke sitemesh inside the original Jersey resource method?</p>
  
  <p>I did found a solution by a "Ted Young" on the web but it's seemingly partial and doesn't work for me (also it requires to use SpringWebMVC, which is not optimal for me at this time).</p>