mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
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/"));
|
||||
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);
|
||||
|
@ -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<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
|
||||
public void createLayoutFromConfigClass() throws Exception {
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
|
Loading…
Reference in New Issue
Block a user