PDA

View Full Version : Spring Freemarker tags in Sitemesh decorators...


lukasz
Oct 5th, 2005, 10:11 AM
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

larry
Oct 18th, 2005, 02:16 AM
The SiteMesh decorator servlet only exposes objects like base and title as described here http://www.opensymphony.com/sitemesh/freemarker-decorators.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.

andrews
Feb 28th, 2006, 11:03 PM
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/>
<#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:


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


<#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! :)