Browse Source

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
pull/4212/head
Stephane Nicoll 10 years ago
parent
commit
8978f54cc3
  1. 3
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java
  2. 9
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java
  3. 2
      spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

3
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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);
}

9
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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/");

2
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. @@ -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

Loading…
Cancel
Save