Browse Source

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
pull/663/head
Dave Syer 12 years ago
parent
commit
0b89402240
  1. 12
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java
  2. 1
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java

12
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

@ -178,6 +178,9 @@ public class ThymeleafAutoConfiguration { @@ -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 { @@ -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

1
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java

@ -84,6 +84,7 @@ public class ThymeleafAutoConfigurationTests { @@ -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

Loading…
Cancel
Save