From 19b5e5923476d14e8fcd90b950bfeca795b67cf0 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 2 Oct 2015 16:12:42 +0200 Subject: [PATCH] Use spring.velocity.charset for template encoding Previously `spring.velocity.charset` only controlled the output encoding with the templates being loaded with the default encoding. We now consistently set the same value for both the input and output encodings. It is still possible to override it to a different value using `spring.velocity.properties.input.encoding` Closes gh-3994 --- .../autoconfigure/velocity/VelocityAutoConfiguration.java | 1 + .../velocity/VelocityAutoConfigurationTests.java | 7 +++++++ .../src/main/asciidoc/appendix-application-properties.adoc | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java index e9ed1172fd..32d9e09a28 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java @@ -88,6 +88,7 @@ public class VelocityAutoConfiguration { factory.setResourceLoaderPath(this.properties.getResourceLoaderPath()); factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess()); Properties velocityProperties = new Properties(); + velocityProperties.setProperty("input.encoding", this.properties.getCharsetName()); velocityProperties.putAll(this.properties.getProperties()); factory.setVelocityProperties(velocityProperties); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java index 7b1cf6e8b5..35ae356185 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java @@ -113,6 +113,13 @@ public class VelocityAutoConfigurationTests { assertThat(response.getContentType(), equalTo("application/json;charset=UTF-8")); } + @Test + public void customCharset() throws Exception { + registerAndRefreshContext("spring.velocity.charset:ISO-8859-1"); + assertThat(this.context.getBean(VelocityConfigurer.class).getVelocityEngine() + .getProperty("input.encoding"), equalTo((Object) "ISO-8859-1")); + } + @Test public void customPrefix() throws Exception { registerAndRefreshContext("spring.velocity.prefix:prefix/"); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index d55d8726b7..58fabb5c47 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -250,7 +250,7 @@ content into your application; rather pick only the properties that you need. spring.velocity.allow-session-override=false spring.velocity.cache=true spring.velocity.check-template-location=true - spring.velocity.charset=UTF-8 + spring.velocity.charset=UTF-8 # charset for input and output encoding spring.velocity.content-type=text/html spring.velocity.date-tool-attribute= spring.velocity.enabled=true # enable MVC view resolution