Raedwald
Raedwald

Reputation: 48624

Have Spring tell FreeMarker the URL encoding to use

I am using FreeMarker with Spring to generate my HTML pages. In my Freemarker page I try to URL escape a String value, like this:

        <a href="<@spring.url "/ext/Thing/${thing?url}/"/>">Thing ${thing}</a>

This results in an exception from FreeMarker that complains

ERROR 2014-02-03 11:08:45,513 Error executing FreeMarker template FreeMarker template error: To do URL encoding, the framework that encloses FreeMarker must specify the output encoding or the URL encoding charset, so ask the programmers to fix it. Or, as a last chance, you can set the url_encoding_charset setting in the template, e.g. <#setting url_escaping_charset='ISO-8859-1'>, or give the charset explicitly to the buit-in, e.g. foo?url('ISO-8859-1').

What to do to fix this problem?

Upvotes: 3

Views: 3925

Answers (1)

Raedwald
Raedwald

Reputation: 48624

First, what "URL encoding charset" to use? That should be UTF-8.

In this context, "the framework that encloses FreeMarker" is Spring, and specifically the org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer bean that must be present to use FreeMarker with Spring. You have to provide a freemarkerSettings Property (key-value mapping) that has a value for the url_escaping_charset property. In the Spring web servlet configuration file (beans configuration file) write something like this:

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

Upvotes: 4

Related Questions