【问题标题】:Character-encoding problem spring字符编码问题春天
【发布时间】:2011-03-05 03:01:32
【问题描述】:

我的网站编码遇到了一个大问题! 我使用 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" />

我在server.xml的元素中将useBodyEncodingForURI属性设置为true:%CATALINA_HOME%/conf,另外一次尝试添加URIEncoding="UTF-8"。

我使用字符集 [utf8] 和集合 [utf8_general_ci] 创建了所有表和字段

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

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

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

  1. 默认情况下应该加载messages.properties(捷克语),而不是默认加载messages_en.properties。

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

不知道哪里错了!为什么我不能让它发挥作用,尽管我做了人们所做的事情并为他们工作!不知道。。

请帮帮我,我被这个糟糕的问题困扰了好几天了,这让我发疯了!

提前谢谢你。

【问题讨论】:

    标签: spring tomcat character-encoding


    【解决方案1】:

    1:默认情况下应该加载messages.properties(捷克语),而不是默认加载messages_en.properties。

    我不使用 Spring,所以这里是在黑暗中拍摄:可能是因为首先订购英语和/或您的平台/浏览器使用英语作为默认语言环境?重新排列配置,检查 HTTP 请求头中的accept-language 以排除一个和另一个。

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

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

    数据访问层是否正确配置为以 UTF-8 格式处理数据?众所周知,MySQL JDBC 驱动程序在这方面有点不聪明。它不会使用 DB 指定的编码,而是使用客户端平台的默认编码。至此,您希望在 JDBC 连接字符串中使用 useUnicode=truecharacterEncoding=UTF-8 属性。例如

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

    【讨论】:

    • 感谢您的建议,关于第一点,我使用了 Spring 的 CoockieLocaleResolver 表单,它将保存用户的偏好以供以后使用。 #2。你是对的,控制台的问题是通过使用eclipse将文本文件编码设置为URF-8来解决的,但是存储在数据库中的数据仍然损坏! #3 问题列表中的问题是,从messages_cz.properties 检索到的数据在网页中没有正确显示,它总是损坏!虽然页面编码是UTF-8!
    【解决方案2】:

    首先,如果您的项目使用 Maven,请确保 Maven Resources Plugin has UTF-8 set as its character encoding scheme,否则消息属性文件可能会以不正确的编码写入您的目标。

    其次,您使用的是 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>
    

    我从 this Cake Solutions 博客文章中发现的。

    【讨论】:

    • 这是银弹 :-)
    • 我在 Spring 应用程序中尝试过这个,但没有成功。
    【解决方案3】:

    如果这仍然不起作用并且您正在使用 Tomcat 作为应用程序服务器,请尝试在 server.xml 中的每个 &lt;Connector&gt; 元素上设置以下选项:

    <Connector URIEncoding="UTF-8" ...>
        ...
    </Connector>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多