From 8978f54cc36361cc2d6c0b9504e9477afe2e4013 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 --- .../velocity/VelocityAutoConfiguration.java | 3 ++- .../velocity/VelocityAutoConfigurationTests.java | 9 ++++++++- .../main/asciidoc/appendix-application-properties.adoc | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) 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 7c08177f84..2e0d4a32ef 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -86,6 +86,7 @@ public class VelocityAutoConfiguration { factory.setResourceLoaderPath(this.properties.getResourceLoaderPath()); factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess()); Properties velocityProperties = new Properties(); + velocityProperties.setProperty("input.encoding", this.properties.getCharset()); 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 974e127b92..916cfbe76c 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -109,6 +109,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 c957bd1ffc..ed962fa320 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -187,7 +187,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.expose-request-attributes=false