Results 1 to 4 of 4

Thread: Spring Freemarker tags in Sitemesh decorators...

  1. #1
    Join Date
    Jul 2005
    Posts
    9

    Default Spring Freemarker tags in Sitemesh decorators...

    Hi.

    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...

    --
    Regards,
    Å?ukasz

  2. #2
    Join Date
    Jun 2005
    Posts
    4

    Default

    The SiteMesh decorator servlet only exposes objects like base and title as described here http://www.opensymphony.com/sitemesh...ecorators.html.

    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. #3
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Lightbulb 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):

    Code:
    <#-- Gets the value of the specified meta tag, or an empty string if it's not set -->
    <#macro meta name>
    	<#assign key="meta." + name/>
    	<#assign value=page.properties[key]?default("")/>${value}</#macro>
    
    <#-- 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 -->
    </#macro>
    3. Each of my view templates starts by invoking the "metadata" macro:

    Code:
    <#import "/spring.ftl" as spring />
    <#import "/Macros.ftl" as macros />
    <@macros.metadata/>
    <#-- 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.:

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

    Code:
    <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. #4

    Default

    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 />

Similar Threads

  1. Using Custom Tags with Spring
    By CaptainMu in forum Web
    Replies: 7
    Last Post: Jul 7th, 2008, 07:00 AM
  2. Sitemesh with Velocity Decorators
    By fsamir in forum Web
    Replies: 13
    Last Post: Oct 9th, 2005, 07:40 AM
  3. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  4. Freemarker Spring Macro problem
    By Gideon in forum Web
    Replies: 15
    Last Post: Jan 25th, 2005, 07:24 AM
  5. Spring + SiteMesh + Velocity?
    By chrisrbailey in forum Web
    Replies: 3
    Last Post: Jan 20th, 2005, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •