You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Richard Wallace <rw...@thewallacepack.net> on 2007/04/25 22:18:23 UTC

Struts2, Sitemesh and Freemarker integration

I've just about got everything worked out for using Freemarker to create 
my Sitemesh templates and have it have access to all my Struts2 
goodness.  To do that I've got the following in my web.xml

  <!-- ======================== SiteMesh Page filter 
======================= -->
  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>
      org.apache.struts2.sitemesh.FreeMarkerPageFilter
    </filter-class>
  </filter>

  <!--Declare servlet for handling freemarker requests -->
  <servlet>
    <servlet-name>sitemesh-freemarker</servlet-name>
    <servlet-class>
      com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet
    </servlet-class>
    <init-param>
      <param-name>TemplatePath</param-name>
      <param-value>class://decorators</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- FreeMarker servlet should serve all requests with .dec extension-->
  <servlet-mapping>
    <servlet-name>sitemesh-freemarker</servlet-name>
    <url-pattern>*.ftl</url-pattern>
  </servlet-mapping>

  <!-- ================ Struts2 dispatching and cleanup filter 
============= -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
  </filter>

  <!-- Allows access to struts2 features from Sitemesh -->
  <filter>
    <filter-name>struts2-cleanup</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.ActionContextCleanUp
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

I think this is _almost_ working, but I'm getting an exception when I 
try and hit a decorated page.  It looks as though the Struts2 Sitemesh 
Freemarker page filter is unable to get the locale to use and so throws 
a NPE. 

12:48:18.182 [btpool0-2] ERROR o.a.s.sitemesh.FreeMarkerPageFilter - 
Error applying decorator: null
java.lang.NullPointerException
        at 
org.apache.struts2.sitemesh.FreeMarkerPageFilter.getLocale(FreeMarkerPageFilter.java:176)
        at 
org.apache.struts2.sitemesh.FreeMarkerPageFilter.applyDecorator(FreeMarkerPageFilter.java:143)
        at 
org.apache.struts2.sitemesh.TemplatePageFilter.applyDecorator(TemplatePageFilter.java:113)
        at 
com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:62)
        at 
org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1040)
        at 
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
        at 
org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1040)

Looking at the offending code, it looks as tho the filter assumes that 
the page being decorated is a Struts2 action and in my particular case 
that's not true.  I also have JSPs and other things that I want to be 
decorated.  For now I can subclass the FreeMarkerPageFilter and override 
the getLocale() method and have it check if invocation.getAction() is 
null  and if it is return the configuration.getLocale().  Would that be 
the correct behaviour?

Thanks,
Rich

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org