Description
Hello! I found an issue with Freemarker support.
The following configuration will use the platform encoding instead of UTF-8 like I expected :
package test.springFreemarker;
import java.nio.charset.StandardCharsets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
@Configuration
public class FreeMarkerConfig {
@Bean
public FreeMarkerViewResolver freemarkerViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setSuffix(".ftl");
return resolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setDefaultEncoding(StandardCharsets.UTF_8.name());
freeMarkerConfigurer.setTemplateLoaderPath("classpath:/templates");
return freeMarkerConfigurer;
}
}
The resulting HTML page had the line :
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
even though my template had no meta tag.
I had to add
resolver.setContentType("text/html;charset=UTF-8");
to make it work.
It seems FreeMarkerConfigurer
default encoding is ignored because FreeMarkerViewResolver
overrides it, even if it was left empty.
The Javadoc of FreeMarkerConfigurer#setDefaultEncoding
mentions it will be ignored if it is explicitly specified by FreeMarkerViewResolver
, but if it was not explicitly specified, it just uses the default.
Either the behavior needs to be changed, or the Javadoc updated to state that FreeMarkerViewResolver
will always override it.
I am using spring-boot-starter-freemarker
with Spring Boot 3.2.5.
Thanks again for the great framework, I hope this issue report helps.