From 0ec2d19e38a6ec0e8c643b05226f5bb36da86816 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 10 Oct 2013 09:58:05 -0400 Subject: [PATCH] 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 --- .../thymeleaf/ThymeleafAutoConfiguration.java | 15 +++++++++++-- .../ThymeleafAutoConfigurationTests.java | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index 43f11335048..3eb659e08f7 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -100,6 +100,8 @@ public class ThymeleafAutoConfiguration { "classpath:/templates/")); resolver.setSuffix(this.environment.getProperty("suffix", ".html")); resolver.setTemplateMode(this.environment.getProperty("mode", "HTML5")); + resolver.setCharacterEncoding(this.environment.getProperty("encoding", + "UTF-8")); resolver.setCacheable(this.environment.getProperty("cache", Boolean.class, true)); return resolver; @@ -144,7 +146,15 @@ public class ThymeleafAutoConfiguration { @Configuration @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 private SpringTemplateEngine templateEngine; @@ -154,7 +164,8 @@ public class ThymeleafAutoConfiguration { public ThymeleafViewResolver thymeleafViewResolver() { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); 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 // InternalResourceViewResolver) resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java index 58d832e7ac5..582732c7d53 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java @@ -34,6 +34,8 @@ import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.spring3.view.ThymeleafView; 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.assertTrue; @@ -63,6 +65,25 @@ public class ThymeleafAutoConfigurationTests { context.close(); } + @Test + public void overrideCharacterEncoding() throws Exception { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(ThymeleafAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + Map map = new HashMap(); + 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 public void createLayoutFromConfigClass() throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();