Add spring.thymeleaf.encoding to ThymeleafAutoConfiguration
Both the template resolver and the view resolver now have their encoding set explicitly (defaulting to UTF-8). Fixes gh-79
This commit is contained in:
parent
2c60828cf2
commit
0ec2d19e38
|
|
@ -100,6 +100,8 @@ public class ThymeleafAutoConfiguration {
|
||||||
"classpath:/templates/"));
|
"classpath:/templates/"));
|
||||||
resolver.setSuffix(this.environment.getProperty("suffix", ".html"));
|
resolver.setSuffix(this.environment.getProperty("suffix", ".html"));
|
||||||
resolver.setTemplateMode(this.environment.getProperty("mode", "HTML5"));
|
resolver.setTemplateMode(this.environment.getProperty("mode", "HTML5"));
|
||||||
|
resolver.setCharacterEncoding(this.environment.getProperty("encoding",
|
||||||
|
"UTF-8"));
|
||||||
resolver.setCacheable(this.environment.getProperty("cache", Boolean.class,
|
resolver.setCacheable(this.environment.getProperty("cache", Boolean.class,
|
||||||
true));
|
true));
|
||||||
return resolver;
|
return resolver;
|
||||||
|
|
@ -144,7 +146,15 @@ public class ThymeleafAutoConfiguration {
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ Servlet.class })
|
@ConditionalOnClass({ Servlet.class })
|
||||||
protected static class ThymeleafViewResolverConfiguration {
|
protected static class ThymeleafViewResolverConfiguration implements EnvironmentAware {
|
||||||
|
|
||||||
|
private RelaxedPropertyResolver environment;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setEnvironment(Environment environment) {
|
||||||
|
this.environment = new RelaxedPropertyResolver(environment,
|
||||||
|
"spring.thymeleaf.");
|
||||||
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SpringTemplateEngine templateEngine;
|
private SpringTemplateEngine templateEngine;
|
||||||
|
|
@ -154,7 +164,8 @@ public class ThymeleafAutoConfiguration {
|
||||||
public ThymeleafViewResolver thymeleafViewResolver() {
|
public ThymeleafViewResolver thymeleafViewResolver() {
|
||||||
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
|
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
|
||||||
resolver.setTemplateEngine(this.templateEngine);
|
resolver.setTemplateEngine(this.templateEngine);
|
||||||
resolver.setCharacterEncoding("UTF-8");
|
resolver.setCharacterEncoding(this.environment.getProperty("encoding",
|
||||||
|
"UTF-8"));
|
||||||
// Needs to come before any fallback resolver (e.g. a
|
// Needs to come before any fallback resolver (e.g. a
|
||||||
// InternalResourceViewResolver)
|
// InternalResourceViewResolver)
|
||||||
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20);
|
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20);
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,8 @@ import org.thymeleaf.TemplateEngine;
|
||||||
import org.thymeleaf.context.Context;
|
import org.thymeleaf.context.Context;
|
||||||
import org.thymeleaf.spring3.view.ThymeleafView;
|
import org.thymeleaf.spring3.view.ThymeleafView;
|
||||||
import org.thymeleaf.spring3.view.ThymeleafViewResolver;
|
import org.thymeleaf.spring3.view.ThymeleafViewResolver;
|
||||||
|
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||||
|
import org.thymeleaf.templateresolver.TemplateResolver;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
@ -63,6 +65,25 @@ public class ThymeleafAutoConfigurationTests {
|
||||||
context.close();
|
context.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void overrideCharacterEncoding() throws Exception {
|
||||||
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
|
context.register(ThymeleafAutoConfiguration.class,
|
||||||
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
|
map.put("spring.thymeleaf.encoding", "UTF-16");
|
||||||
|
context.getEnvironment().getPropertySources()
|
||||||
|
.addFirst(new MapPropertySource("test", map));
|
||||||
|
context.refresh();
|
||||||
|
context.getBean(TemplateEngine.class).initialize();
|
||||||
|
ITemplateResolver resolver = context.getBean(ITemplateResolver.class);
|
||||||
|
assertTrue(resolver instanceof TemplateResolver);
|
||||||
|
assertEquals("UTF-16", ((TemplateResolver) resolver).getCharacterEncoding());
|
||||||
|
ThymeleafViewResolver views = context.getBean(ThymeleafViewResolver.class);
|
||||||
|
assertEquals("UTF-16", views.getCharacterEncoding());
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createLayoutFromConfigClass() throws Exception {
|
public void createLayoutFromConfigClass() throws Exception {
|
||||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue