字符编码问题 Spring

14

我在网站编码方面遇到了大问题! 我使用的是Spring 3、Tomcat 6和MySQL数据库。我想在我的网站上支持德语和捷克语,同时还有英语。我将所有的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" />

我在 %CATALINA_HOME%/conf 的 server.xml 文件的 Connector 元素中将 useBodyEncodingForURI 属性设置为 true,另外也尝试过使用 URIEncoding="UTF-8"。

我创建了所有的表和字段都使用了字符集 utf8 和校对规则 utf8_general_ci。

我的浏览器编码是 UTF-8(顺便说一句,我使用的是 IE8 和 Firefox 3.6.3)。

当我在 MYSQL Query 浏览器中手动插入捷克语或德语数据时,它被正确地插入,并且在我的应用程序中也正确地显示出来。

所以,以下是我遇到的问题列表:

  1. 默认情况下,应该加载 messages.properties(捷克语),但实际上默认加载的是 messages_en.properties。

  2. 在 web 表单中,当我输入捷克语数据并单击提交按钮时,在 Controller 中将数据打印到控制台之前,不正确的字符会被打印出来,这正是保存到数据库中的确切数据。

我不知道哪里出错了!为什么我做了别人做过并且成功的事情,但是我却无法让它工作!我不知道。。。

请帮帮我,我被这个烂问题困扰了几天,让我疯狂!

非常感谢您的帮助。

3个回答

15

首先,如果您的项目使用Maven,请确保Maven资源插件将UTF-8设置为其字符编码方案,否则消息属性文件可能会以不正确的编码写入您的目标文件。

其次,您正在使用ResourceBundleMessageSource,该组件使用标准的java.util.ResourceBundlejava.util.Properties,仅支持ISO-8859-1编码。您可以改用ReloadableResourceBundleMessageSource,如下所示:

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

我从这篇Cake Solutions博客文章中发现了它。


这就是银弹的效果 :-) - user256872
我在Spring应用程序中尝试过,但没有成功。 - Raphaël Colantonio

3
如果这仍然不起作用,并且您使用的是Tomcat应用程序服务器,请尝试在server.xml中的每个<Connector>元素上设置以下选项:
<Connector URIEncoding="UTF-8" ...>
    ...
</Connector>

这对我很有帮助。其他应用服务器可能有类似的选项,因此您可能需要检查服务器文档。

2

1:默认情况下,应加载messages.properties(捷克语),但实际上默认加载了messages_en.properties(英语)。

我不熟悉Spring,所以这只是一个猜测:是否因为英语排序在前,或者您的平台/浏览器将英语作为默认语言环境?重新排列配置并检查HTTP请求头中的`accept-language`以排除其他语言环境。

2:在Web表单中,当我输入捷克语数据,然后单击提交,在控制器中,在将其保存到数据库之前,在控制台中打印出数据,打印出的内容不正确,带有奇怪的字符,而这正是保存到数据库中的确切数据。

是否已将控制台配置为使用UTF-8来显示数据?否则,它将使用平台默认编码。例如,在Eclipse中,您可以通过“Window > Preferences > General > Workspace > Text File Encoding”进行配置。

数据访问层是否已正确配置以处理UTF-8格式的数据?MySQL JDBC驱动程序众所周知有点不 smart。它不会使用数据库指定的编码,而是客户端平台默认的编码。因此,您需要在JDBC连接字符串中使用`useUnicode=true`和`characterEncoding=UTF-8`属性。例如:

jdbc:mysql://localhost/some_db?useUnicode=yes&characterEncoding=UTF-8

谢谢您的建议,关于第一点,我使用了Spring的CoockieLocaleResolver,它将保存用户的偏好设置以供以后使用。#2. 您是正确的,通过在Eclipse中将文本文件编码设置为URF-8,控制台的问题已经解决,但仍然存在数据库中存储的数据损坏的问题!#3 将问题列表中的问题添加到其中,即从messages_cz.properties检索到的数据在网页中无法正确显示,总是损坏的!尽管页面编码为UTF-8! - user256872

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接