I have Java webapp that uses Spring 3.1, with Freemarker templates for rendering the view. I want to conditionally display a link in the view, based on the true/false value of a particular application property.
I have the following app property defined in src/main/resources/application.properties:
showLink=true
If I were using a regular JSP with Spring MVC, I could use SpEL to conditionally display the link based on the value of showLink:
<c:if test="${configuration['showLink']}">
<a href="...">some link</a>
</c:if>
How do I do this in a Freemarker template? I tried doing something like this, but couldn't get it to work:
<#assign showLink>${configuration['showLink']}</#assign>
<#if showHelpLink>
<a href="...">some link</a>
</#if>
I looked at the Spring freemarker macros (in in spring.ftl), but the closest thing I see is the ability to get a message property, not an app property.
Things I've tried that didn't work
I looked at the macros in spring.ftl, but the closest it comes is giving me message properties.
Also, I can't inject the value into the controller and then put it into the
ModelMap, because my FreeMarker template is the header for all pages so it's auto-imported:
<bean id="abstractFreemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" abstract="true">
...
<property name="freemarkerSettings">
<props>
<prop key="auto_import">
/spring.ftl as spring, /myTemplate.ftl as myTemplate
</prop>
</props>
</property>
...
</bean>
Things I haven't tried yet
I found a blog post describing how to manually add support for SpEL to Freemarker. I'd rather not do all of that for this one case where I need it.
Creating a custom tag library to retrieve the application property value, so I could do something like this in my freemarker template:
<#assign showLink><foo:getAppProperty name="showLink"/></#assign>