Add spring.thymeleaf.contentType (defaults to HTML)
User can specify the content type in external properties now, optionally ommitting the charset (since that is duplicated). If charset is not appended by user Spring will do it. Fixes gh-671
This commit is contained in:
@@ -178,6 +178,9 @@ public class ThymeleafAutoConfiguration {
|
||||
resolver.setTemplateEngine(this.templateEngine);
|
||||
resolver.setCharacterEncoding(this.environment.getProperty("encoding",
|
||||
"UTF-8"));
|
||||
resolver.setContentType(addEncoding(
|
||||
this.environment.getProperty("contentType", "text/html"),
|
||||
resolver.getCharacterEncoding()));
|
||||
resolver.setExcludedViewNames(this.environment.getProperty(
|
||||
"excludedViewNames", String[].class));
|
||||
resolver.setViewNames(this.environment.getProperty("viewNames",
|
||||
@@ -188,6 +191,15 @@ public class ThymeleafAutoConfiguration {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
private String addEncoding(String type, String charset) {
|
||||
if (type.contains("charset=")) {
|
||||
return type;
|
||||
}
|
||||
else {
|
||||
return type + ";charset=" + charset;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
@@ -84,6 +84,7 @@ public class ThymeleafAutoConfigurationTests {
|
||||
assertEquals("UTF-16", ((TemplateResolver) resolver).getCharacterEncoding());
|
||||
ThymeleafViewResolver views = this.context.getBean(ThymeleafViewResolver.class);
|
||||
assertEquals("UTF-16", views.getCharacterEncoding());
|
||||
assertEquals("text/html;charset=UTF-16", views.getContentType());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user