From dd748eda19f4e94ba5701dff45a006f00842a550 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 26 Mar 2015 17:55:17 +0000 Subject: [PATCH 1/2] Use the configured charset, if any, in MustacheViewResolver Closes gh-2670 --- .../mustache/MustacheAutoConfiguration.java | 1 + .../mustache/web/MustacheViewResolver.java | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java index 819d957b615..cdca7995d07 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java @@ -108,6 +108,7 @@ public class MustacheAutoConfiguration { resolver.setCache(this.mustache.isCache()); resolver.setViewNames(this.mustache.getViewNames()); resolver.setContentType(this.mustache.getContentType()); + resolver.setCharset(this.mustache.getCharset()); resolver.setCompiler(mustacheCompiler); resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); return resolver; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java index 00097dd9a38..022c55c8cf8 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java @@ -34,12 +34,15 @@ import com.samskivert.mustache.Template; * Spring MVC {@link ViewResolver} for Mustache. * * @author Dave Syer + * @author Andy Wilkinson * @since 1.2.2 */ public class MustacheViewResolver extends UrlBasedViewResolver { private Compiler compiler = Mustache.compiler(); + private String charset; + public MustacheViewResolver() { setViewClass(MustacheView.class); } @@ -51,6 +54,13 @@ public class MustacheViewResolver extends UrlBasedViewResolver { this.compiler = compiler; } + /** + * @param charset the charset to set + */ + public void setCharset(String charset) { + this.charset = charset; + } + @Override protected View loadView(String viewName, Locale locale) throws Exception { Resource resource = resolveResource(viewName, locale); @@ -64,7 +74,9 @@ public class MustacheViewResolver extends UrlBasedViewResolver { } private Template createTemplate(Resource resource) throws IOException { - return this.compiler.compile(new InputStreamReader(resource.getInputStream())); + return this.charset == null ? this.compiler.compile(new InputStreamReader( + resource.getInputStream())) : this.compiler + .compile(new InputStreamReader(resource.getInputStream(), this.charset)); } private Resource resolveResource(String viewName, Locale locale) { From 0da24f82e32433326db47e9d6f1a0e57ef01e827 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 26 Mar 2015 17:59:13 +0000 Subject: [PATCH 2/2] Polishing: move tests for mustache.web into the correct package --- .../mustache/{ => web}/MustacheViewResolverTests.java | 2 +- .../autoconfigure/mustache/{ => web}/MustacheViewTests.java | 2 +- .../mustache/{ => web}/MustacheWebIntegrationTests.java | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) rename spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/{ => web}/MustacheViewResolverTests.java (97%) rename spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/{ => web}/MustacheViewTests.java (97%) rename spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/{ => web}/MustacheWebIntegrationTests.java (93%) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheViewResolverTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolverTests.java similarity index 97% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheViewResolverTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolverTests.java index 987a175f776..8df86ea06d4 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheViewResolverTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolverTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache; +package org.springframework.boot.autoconfigure.mustache.web; import java.util.Locale; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheViewTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewTests.java similarity index 97% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheViewTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewTests.java index 9587ff0920d..8f91c6b680b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheViewTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache; +package org.springframework.boot.autoconfigure.mustache.web; import java.util.Collections; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheWebIntegrationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheWebIntegrationTests.java similarity index 93% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheWebIntegrationTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheWebIntegrationTests.java index 136e549084e..121f91bff1e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheWebIntegrationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheWebIntegrationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.mustache; +package org.springframework.boot.autoconfigure.mustache.web; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; @@ -31,9 +31,11 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.autoconfigure.mustache.MustacheWebIntegrationTests.Application; +import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration; +import org.springframework.boot.autoconfigure.mustache.MustacheResourceTemplateLoader; import org.springframework.boot.autoconfigure.mustache.web.MustacheView; import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver; +import org.springframework.boot.autoconfigure.mustache.web.MustacheWebIntegrationTests.Application; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;