Announcement Announcement Module
No announcement yet.
Spring Freemarker tags in Sitemesh decorators... Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Freemarker tags in Sitemesh decorators...


    I am using Freemarker for Sitemesh decorators. I want to use Spring Freemarker tags (like <@spring.message />) in Sitemesh decorators but I get: "Expression springMacroRequestContext is undefined .... The problematic instruction:
    ==> ${springMacroRequestContext.getMessage(code)} ". I also want to know if it is possible to put data model to decorator...


  • #2
    The SiteMesh decorator servlet only exposes objects like base and title as described here

    I guess this needs to be fixed in SiteMesh. But you can build your own decorators also.

    Spring's own FreeMarkerViewResolver can be configured to expose springMacroRequestContext among other things.


    • #3
      My somewhat clunky solution

      I too am using SiteMesh to decorate my FreeMarker views. Here's how I worked around the problem:

      1. Defined these two Freemarker macros (my macros file is called Macros.ftl, but you could use any name):

      <#-- Gets the value of the specified meta tag, or an empty string if it's not set -->
      <#macro meta name>
      	<#assign key="meta." + name/>
      <#-- Adds HTML meta tags containing the Strings needed by the layout page -->
      <#macro metadata>
          <meta name="some.key" content="${some_FTL_expression}"/>
          <meta name="some.other.key" content="<@spring.message code="some_Message_Code"/>"/>
          <meta name="yet.another.key" content="<@macros someOtherMacro/>"/>
          <#-- other values as needed -->
      3. Each of my view templates starts by invoking the "metadata" macro:

      <#import "/spring.ftl" as spring />
      <#import "/Macros.ftl" as macros />
      <#-- Rest of template goes here -->
      This causes each view to read the values/expressions listed in the "metadata" macro and output them as HTML <meta> tags. In my case, these values are things like locale-sensitive messages, the username, my company name, and the name and version of my application

      4. Then in my layout template (the one used by SiteMesh to perform the decoration), I read each of those HTML meta tags where necessary using the first macro, e.g.:

      <#import "/Macros.ftl" as macros />
      <p><@macros.meta name="some.other.key"/></p>
      which at runtime generates the following HTML snippet:

      <p>Hello World!</p>
      Where "Hello World!" was the resource bundle translation of the "some_Message_Code" message code. The downside of this solution is that I have to edit the "metadata" macro every time I add a new piece of text to the layout template. If you come across a neater solution, please let me know!


      • #4
        Simply copy spring-webmvc-3.0.5.RELEASE.jar\org\springframework\web\servlet\ view\freemarker\spring.ftl to the same location of sitemesh ftl file. and use import with backslash before spring.ftl as below
        <#import "/spring.ftl" as spring />