Spring boot 1.2.5.RELEASE using velocity template Chinese garbled problem

Source: Internet
Author: User

Application.properties file:

spring.velocity.resourceloaderpath=classpath:/templates/
spring.velocity.prefix=
Spring.velocity.suffix=.vm
Spring.velocity.cache=false
Spring.velocity.check-template-location=true
Spring.velocity.charset=utf-8
Spring.velocity.content-type=text/html

In the spring boot development, although the encoding is set, but this version of spring boot will still produce Chinese garbled, but using the Freemarker template is not a problem, The workaround is to write a Velocityautoconfiguration.java file in the project, overwriting the velocityautoconfiguration class in spring boot, the package name:

Org.springframework.boot.autoconfigure.velocity;

The file content is copied from the spring boot source and placed in its own project, the package name and file name, the modified content only need to add a line of code in applyproperties :


Velocityproperties.setproperty ("Input.encoding", This.properties.getCharset ());
The modified Applyproperties method is as follows:
protected void Applyproperties (Velocityenginefactory factory) {Factory.setresourceloaderpath ( This.properties.getResourceLoaderPath ()); Factory.setpreferfilesystemaccess ( This.properties.isPreferFileSystemAccess ()); Properties Velocityproperties = new properties (); Velocityproperties.putall (This.properties.getProperties ()); Velocityproperties.setproperty ("Input.encoding", This.properties.getCharset ()); Factory.setvelocityproperties ( velocityproperties);}


The full contents of the file can be saved to the project:

/* Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */package Org.springframework.boot.autoconfigure.velocity;import Java.io.ioexception;import java.util.Properties; Import Javax.annotation.postconstruct;import Javax.servlet.servlet;import org.apache.velocity.app.VelocityEngine; Import Org.apache.velocity.exception.velocityexception;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.boot.autoconfigure.AutoConFigureafter;import Org.springframework.boot.autoconfigure.enableautoconfiguration;import Org.springframework.boot.autoconfigure.condition.conditionalonclass;import Org.springframework.boot.autoconfigure.condition.conditionalonmissingbean;import Org.springframework.boot.autoconfigure.condition.conditionalonnotwebapplication;import Org.springframework.boot.autoconfigure.condition.conditionalonproperty;import Org.springframework.boot.autoconfigure.condition.conditionalonwebapplication;import Org.springframework.boot.autoconfigure.template.templatelocation;import Org.springframework.boot.autoconfigure.web.webmvcautoconfiguration;import Org.springframework.boot.context.properties.enableconfigurationproperties;import Org.springframework.boot.web.servlet.view.velocity.embeddedvelocityviewresolver;import Org.springframework.context.applicationcontext;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.ui.veloCity. Velocityenginefactory;import Org.springframework.ui.velocity.velocityenginefactorybean;import Org.springframework.util.assert;import Org.springframework.web.servlet.view.velocity.velocityconfig;import Org.springframework.web.servlet.view.velocity.velocityconfigurer;import org.springframework.web.servlet.view.velocity.velocityviewresolver;/** * {@link enableautoconfiguration Auto-configuration} for Velocity. * * @author Andy Wilkinson * @since 1.1.0 */@Configuration @conditionalonclass ({velocityengine.class, Velocityenginefactory.class}) @AutoConfigureAfter (Webmvcautoconfiguration.class) @EnableConfigurationProperties ( Velocityproperties.class) public class Velocityautoconfiguration {@Autowired private applicationcontext Applicationc    Ontext;    @Autowired private Velocityproperties properties;            @PostConstruct public void checktemplatelocationexists () {if (This.properties.isCheckTemplateLocation ()) {    Templatelocation location = new Templatelocation (                This.properties.getResourceLoaderPath ());                            Assert.state (Location.exists (This.applicationcontext), "Cannot find template location:" + location  + "(please add some templates, check your Velocity" + "configuration,                            or set spring.velocity. "        + "Checktemplatelocation=false)");         }} protected static class Velocityconfiguration {@Autowired protected velocityproperties properties; protected void Applyproperties (Velocityenginefactory factory) {Factory.setresourceloaderpath (this.prop            Erties.getresourceloaderpath ());            Factory.setpreferfilesystemaccess (This.properties.isPreferFileSystemAccess ());            Properties Velocityproperties = new properties ();            Velocityproperties.putall (This.properties.getProperties ());   Velocityproperties.setproperty ("Input.encoding", This.properties.getCharset ());         Factory.setvelocityproperties (velocityproperties); }} @Configuration @ConditionalOnNotWebApplication public static class Velocitynonwebconfiguration extends Vel ocityconfiguration {@Bean @ConditionalOnMissingBean public velocityenginefactorybean Velocityconfigu            Ration () {Velocityenginefactorybean Velocityenginefactorybean = new Velocityenginefactorybean ();            Applyproperties (Velocityenginefactorybean);        return Velocityenginefactorybean; }} @Configuration @ConditionalOnClass (servlet.class) @ConditionalOnWebApplication public static class Velo        Citywebconfiguration extends Velocityconfiguration {@Bean @ConditionalOnMissingBean (velocityconfig.class) Public Velocityconfigurer Velocityconfigurer () {velocityconfigurer configurer = new Velocityconfigurer ()            ;            Applyproperties (Configurer);        return configurer; } @Bean Public Velocityengine velocityengine (Velocityconfigurer configurer) throws Velocityexception, IOException {        return Configurer.getvelocityengine (); } @Bean @ConditionalOnMissingBean (name = "Velocityviewresolver") @ConditionalOnProperty (name = "Sprin G.velocity.enabled ", matchifmissing = True) public Velocityviewresolver Velocityviewresolver () {Embedde            Dvelocityviewresolver resolver = new Embeddedvelocityviewresolver ();            This.properties.applyToViewResolver (resolver);        return resolver; }    }}





Spring boot 1.2.5.RELEASE using velocity template Chinese garbled problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.