Spring Boot Mustache无法正确呈现UTF-8

时间:2015-06-04 03:52:36

标签: utf-8 spring-boot mustache

根据官方文档,字符串默认使用UTF-8编码。我调查了自动配置的MustacheViewResolver和MustacheResourceTemplateLoader,它们都将charset属性设置为" UTF-8",但当我添加一个中文单词"中文"时,它没有' ; t正确显示。

有人知道解决方案吗?提前谢谢。

这是pom.xml(仅仅是官方示例):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>
    <artifactId>spring-boot-sample-web-mustache</artifactId>
    <name>spring-boot-sample-web-mustache</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

代码:

//main class
@SpringBootApplication
public class SampleWebMustacheApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleWebMustacheApplication.class, args);
    }

}

//controller
@Controller
public class WelcomeController {
    private String message = "Hello World";

    @RequestMapping("/home")
    public String home(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", this.message);

        return "home";
    }
}

和模板,中文单词&#34;中文&#34;在其中:

<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        Date: {{time.date}}
    <br>
    Time: {{time.time}}
    <br>
    Message: {{message}}中文
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我已经通过升级到最新版本的spring boot解决了这个问题。

只需更新pom.xml即可修改版本号:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>

1.2.5是最新的稳定版本(截至本文撰写时)。