title: How to access a Spring application property in a Freemarker template?
body: <p>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.</p>
  
  <p>I have the following app property defined in <code>src/main/resources/application.properties</code>:</p>
  
  <p><code>showLink=true</code></p>
  
  <p>If I were using a regular JSP with Spring MVC, I could use SpEL to conditionally display the link based on the value of <code>showLink</code>:</p>
  
  
  
  <pre class="lang-html prettyprint-override"><code>&lt;c:if test="${configuration['showLink']}"&gt;
      &lt;a href="..."&gt;some link&lt;/a&gt;
  &lt;/c:if&gt;
  </code></pre>
  
  <p>How do I do this in a Freemarker template? I tried doing something like this, but couldn't get it to work:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;#assign showLink&gt;${configuration['showLink']}&lt;/#assign&gt;
  
  &lt;#if showHelpLink&gt;
      &lt;a href="..."&gt;some link&lt;/a&gt;
  &lt;/#if&gt;
  </code></pre>
  
  <p>I looked at the <a href="http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/view.html#views-form-macros" rel="nofollow">Spring freemarker macros</a> (in in spring.ftl), but the closest thing I see is the ability to get a message property, not an app property.</p>
  
  <h2>Things I've tried that didn't work</h2>
  
  <ol>
  <li><p>I looked at the macros in spring.ftl, but the closest it comes is giving me message properties. </p></li>
  <li><p>Also, I can't inject the value into the controller and then put it into the <code>ModelMap</code>, because my FreeMarker template is the header for all pages so it's auto-imported:</p></li>
  </ol>
  
  
  
  <pre class="lang-html prettyprint-override"><code>&lt;bean id="abstractFreemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" abstract="true"&gt;
      ...
      &lt;property name="freemarkerSettings"&gt;
          &lt;props&gt;
              &lt;prop key="auto_import"&gt;
                  /spring.ftl as spring, /myTemplate.ftl as myTemplate
              &lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
      ...
  &lt;/bean&gt;
  </code></pre>
  
  <h2>Things I haven't tried yet</h2>
  
  <ol>
  <li><p>I found a <a href="http://adaptiweb.blogspot.sk/2012/10/freemarker-template-with-spring.html" rel="nofollow">blog post</a> 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. </p></li>
  <li><p>Creating a custom tag library to retrieve the application property value, so I could do something like this in my freemarker template:</p></li>
  </ol>
  
  
  
  <pre class="lang-html prettyprint-override"><code>&lt;#assign showLink&gt;&lt;foo:getAppProperty name="showLink"/&gt;&lt;/#assign&gt;
  </code></pre>