From 0b89402240d3adbd8dafa387e240b65808b2d65c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 15 Apr 2014 07:10:08 -0700 Subject: [PATCH] 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 --- .../thymeleaf/ThymeleafAutoConfiguration.java | 12 ++++++++++++ .../thymeleaf/ThymeleafAutoConfigurationTests.java | 1 + 2 files changed, 13 insertions(+) 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 fbaa5c67bdc..b7fce9dd15a 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 @@ -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 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 7f2724a85a4..a344fe81eed 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 @@ -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