2017-05-04 173 views
0

我有一個web應用程序使用Spring MVC,版本4.3.8.RELEASE。它保存Unicode字符不正確:Spring MVC顯示Unicode字符不正確

enter image description here

我已經檢查過很多觀點:

(1)在MySQL數據庫中,它是NVARCHAR,不是VARCHAR

(2)在pom.xml,我添加

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 

(3)在JSP頁面中,我添加

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 

(4)內部web.xml,我添加

<filter> 
    <filter-name>encodingFilter</filter-name> 
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter 
    </filter-class> 
    <init-param> 
     <param-name>encoding</param-name> 
     <param-value>UTF-8</param-value> 
    </init-param> 
    <init-param> 
     <param-name>forceEncoding</param-name> 
     <param-value>true</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>encodingFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

(5)在豆控制消息,我添加

<beans:bean id="messageSource" 
      class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <beans:property name="basename" value="META-INF/messages/messages"/> 
    <beans:property name="defaultEncoding" value="UTF-8"/> 
</beans:bean> 

(6)在ViewResolver的,我還用於添加屬性UTF- 8 ...

但是不成功。如何解決它?

這是我的項目:https://github.com/donhuvy/temp_unicode

+0

首先,你可以嘗試使用ReloadableResourceBundleMessageSource類嗎?如果你想 –

+0

我也可以爲'ReloadableResourceBundleMessageSource'添加配置,但是編譯錯誤,我可以與你共享一個工作配置。 –

+0

您是否嘗試刪除org.springframework.web.filter.CharacterEncodingFilter? –

回答

0

問號告訴我(在這種情況下),當它被插入到表中的數據被截斷。也就是說,越南的字符丟失了。

現在,要弄清楚做什麼,而不是。前往Trouble with utf8 characters; what I see is not what I stored

  • 閱讀「最佳實踐」;它可能會指出做錯了什麼。
  • 閱讀「問號」;它提供了問題的可能原因。
  • SELECT HEX(...)...;當正確存儲(到utf8或utf8mb4)時,將爲Điện lạnh,將爲C490 69 E1BB87 6E 20 6C E1BAA1 6E 68

在您的評論Ä?iá»?n lạnh - 是從表中取出後?如果是這樣,那就太晚了。