我在我的網站編碼存在一個大問題! 我使用spring 3,tomcat 6和mysql db。我想支持和英語一塊兒德國和捷克在我的網站,我創建了所有的JSP作爲UTF-8的文件,並在每個JSP我包括以下內容:字符編碼問題春天
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
我創建messages.properties(默認其是捷克語),messages_de.properties和messages_en.properties。所有這些都保存爲UTF-8文件。
我增加了以下內容的web.xml:
<filter>
<filter-name>encodingFilter</filter-name>
<filterclass>
org.springframework.web.filter.CharacterEncodingFilter</filterclass>
<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>
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>en</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
<locale-encoding-mapping>
<locale>cz</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
<locale-encoding-mapping>
<locale>de</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
,並添加以下到我的applicationContext.xml:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basenames="messages"/>
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="locale" />
</mvc:interceptors>
<!-- Declare the Resolver -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
我設置useBodyEncodingForURI屬性爲true,服務器的元素。 xml在%CATALINA_HOME%/ conf下,另一次嘗試添加URIEncoding =「UTF-8」。
我創建了所有的表和字段與字符集[UTF-8]和集合[utf8_general_ci]
在瀏覽器中的編碼是UTF-8(順便說一句,我有IE8和Firefox 3.6.3)
當我打開MYSQL Query瀏覽器並手動插入捷克語或德語數據時,它正確插入,並且正確顯示在我的應用程序中。
所以,這裏的問題的名單上有:
默認情況下,messages.properties(捷克)應該被默認加載,而不是messages_en.properties負荷。
在Web表單,當我進入捷克的數據,然後點擊提交,在控制器我打印出控制檯中的數據之前將其保存到數據庫,什麼正在印刷是不正確的有奇怪的字符,這是保存到數據庫的確切數據。
我不知道錯在哪裏!爲什麼我不能讓它工作,雖然我做了人們爲他們做的工作!不知道..
請幫助我,我被困在這個蹩腳的問題,因爲天,它讓我瘋狂!
預先感謝您。
這是Silver Bullet :-) – user256872 2010-06-24 09:40:51
我在Spring應用程序中嘗試這個,但沒有成功。 – 2016-06-09 02:39:54