From dd748eda19f4e94ba5701dff45a006f00842a550 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 26 Mar 2015 17:55:17 +0000 Subject: [PATCH] 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) {