2015-06-04 57 views
0

根據官方文檔,字符串默認使用UTF-8編碼。我調查了自動構造的MustacheViewResolver和MustacheResourceTemplateLoader,它們都將charset屬性設置爲「UTF-8」,但是當我添加中文單詞「中文」時,它無法正確顯示。Spring Boot Mustache不能正確呈現UTF-8

有沒有人知道解決方案?提前致謝。

下面是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"; 
    } 
} 

和模板,與中國字 「中文」,在它:

<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> 

回答

0

我已經通過升級到最新版本的spring啓動來解決了這個問題。

簡單地更新pom.xml的修改版本號:

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

1.2.5是最新的穩定版本(在本文寫作)。