Search code examples
springfreemarker

Freemarker 2.3.24 auto-escape and spring.ftl macros issue


I upgraded to Freemarker 2.3.24 in order to use the setting output_format as HTMLOutputFormat and enable the auto escape but when I use the spring.ftl to read values from property files I get "Using ?html (legacy escaping) is not allowed when auto-escaping is on with a markup output format (HTML), to avoid double-escaping mistakes." Does anyone knows how to integrate the Freemarker auto-escape with spring property file reader?

here is my config bean:

<bean id="freeMarkerConfigurer"
      class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/views/"/>
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="freemarkerSettings">
        <props>
            <prop key="output_format">HTMLOutputFormat</prop>
        </props>
    </property>
</bean>

and here is my test.ftl

<#import "/spring.ftl" as spring/>
<html>
<div>hello</div>
<p><@spring.message "welcome"/></p>
</html>

and I get this Error:

Using ?html (legacy escaping) is not allowed when auto-escaping is on with a markup output format (HTML), to avoid double-escaping mistakes.

Solution

  • As you have some "legacy" templates (from Spring), you should leave the global output_format alone. Instead, you should specify the output_format for the non-legacy templates only. That can be done in two ways. One is using "ftlh" file extension instead of "ftl" (assuming that you want HTML-escaping) and then setting recognize_standard_file_extensions to true. The other is using the template_configurations setting (see http://freemarker.org/docs/pgui_config_templateconfigurations.html) to specify some other name pattern to associate the output_format with (such as anything that doesn't match the Spring templates).