简体   繁体   English

字符编码问题春天

[英]Character-encoding problem spring

I am stuck in a big problem with encoding in my website! 我在网站上遇到编码的大问题! I use spring 3, tomcat 6, and mysql db. 我使用spring 3,tomcat 6和mysql db。 I want to support German and Czech along with English in my website, I created all the JSPs as UTF-8 files, and in each jsp I include the following: 我想在我的网站上支持德语和捷克语以及英语,我将所有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">

I created messages.properties (the default which is Czech), messages_de.properties, and messages_en.properties. 我创建了messages.properties(默认为捷克语),messages_de.properties和messages_en.properties。 And all of them are saved as UTF-8 files. 所有这些都保存为UTF-8文件。

I added the following to web.xml: 我在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>  

And add the following to my applicationContext.xml: 并将以下内容添加到我的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" />

I set the useBodyEncodingForURI attribute to true in the element of server.xml under: %CATALINA_HOME%/conf, also another time tried to add URIEncoding="UTF-8" instead. 我在server.xml的元素中将useBodyEncodingForURI属性设置为true:%CATALINA_HOME%/ conf,另一次尝试添加URIEncoding =“UTF-8”。

I created all the tables and fields with charset [utf8] and collection [utf8_general_ci] 我使用charset [utf8]和集合[utf8_general_ci]创建了所有表和字段

The encoding in my browser is UTF-8 (BTW, I have IE8 and Firefox 3.6.3) 我的浏览器中的编码是UTF-8(BTW,我有IE8和Firefox 3.6.3)

When I open the MYSQL Query browser and insert manually Czech or German data, it's being inserted correctly, and displayed correctly in my app as well. 当我打开MYSQL查询浏览器并手动插入捷克语或德语数据时,它正确插入,并在我的应用程序中正确显示。

So, here's the list of problems I have: 所以,这是我遇到的问题清单:

  1. By default the messages.properties (Czech) should load, instead the messages_en.properties loads by default. 默认情况下,应加载messages.properties(Czech),而默认情况下会加载messages_en.properties。

  2. In the web form, when I enter Czech data, then click submit, in the Controller I print out the data in the console before to save it to db, what's being printed is not correct having strange chars, and this is the exact data that saves to db. 在Web表单中,当我输入捷克数据,然后单击提交,在控制器中我打印出控制台中的数据,然后将其保存到数据库,打印的内容不正确有奇怪的字符,这是确切的数据,保存到db。

I don't know where's the mistake! 我不知道哪里出错了! Why can't I get it working although I did what people did and worked for them! 为什么我不能让它工作,虽然我做了人们为他们所做的工作! don't know.. 不知道..

Please help me, I am stuck in this crappy problem since days, and it drives me crazy! 请帮助我,我几天都陷入这个糟糕的问题,它让我发疯!

Thank you in advance. 先感谢您。

First, if your project is using Maven, make sure that the Maven Resources Plugin has UTF-8 set as its character encoding scheme , otherwise the message properties files could be written to your target with an incorrect encoding. 首先,如果您的项目使用Maven,请确保Maven Resources Plugin将UTF-8设置为其字符编码方案 ,否则可能会使用不正确的编码将消息属性文件写入目标。

Second, you're using ResourceBundleMessageSource , which uses the standard java.util.ResourceBundle and java.util.Properties , which only support ISO-8859-1 encoding. 其次,您正在使用ResourceBundleMessageSource ,它使用标准的java.util.ResourceBundlejava.util.Properties ,它们仅支持ISO-8859-1编码。 You can instead use ReloadableResourceBundleMessageSource like: 您可以改为使用ReloadableResourceBundleMessageSource

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

which I discovered from this Cake Solutions blog post. 我在Cake Solutions博客文章中发现了这一点

If this still does not work and you're using Tomcat as your application server try to set the following option on every <Connector> element in the server.xml : 如果这仍然不起作用并且您使用Tomcat作为应用程序服务器,请尝试在server.xml中的每个<Connector>元素上设置以下选项:

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

This did the trick for me. 这对我有用。 There might be similar options for other application servers, so you might want to check the server documentation. 其他应用程序服务器可能有类似的选项,因此您可能需要检查服务器文档。

1: By default the messages.properties (Czech) should load, instead the messages_en.properties loads by default. 1:默认情况下,应加载messages.properties(Czech),而默认情况下会加载messages_en.properties。

I don't do Spring, so here's a shoot in the dark: maybe because English is ordered first and/or your platform/browser uses English as default locale? 我不做春天,所以这是在黑暗中拍摄:也许是因为英语是先订购和/或你的平台/浏览器使用英语作为默认语言环境? Rearrange the configuration and check accept-language in the HTTP request header to exclude one and other. 重新排列配置并检查HTTP请求标头中的accept-language以排除其中一个。

2: In the web form, when I enter Czech data, then click submit, in the Controller I print out the data in the console before to save it to db, what's being printed is not correct having strange chars, and this is the exact data that saves to db. 2:在Web表单中,当我输入捷克数据时,然后单击提交,在控制器中我打印出控制台中的数据,然后将其保存到数据库,打印的内容不正确有奇怪的字符,这就是确切的保存到db的数据。

Is the console configured to use UTF-8 to display data? 控制台是否配置为使用UTF-8显示数据? It would use the platform default encoding otherwise. 否则它将使用平台默认编码。 In for example Eclipse you can configure it by Window > Preferences > General > Workspace > Text File Encoding . 在Eclipse中,您可以通过Window> Preferences> General> Workspace> Text File Encoding对其进行配置

Is the data access layer configured properly to handle the data as UTF-8? 是否正确配置了数据访问层以将数据处理为UTF-8? MySQL JDBC driver is known to be a bit non-smart in this. 众所周知,MySQL JDBC驱动程序在这方面有点不聪明。 It won't use the DB-specified encoding, but the client platform default encoding. 它不会使用DB指定的编码,而是使用客户端平台的默认编码。 To the point, you'd like to use the useUnicode=true and characterEncoding=UTF-8 properties in the JDBC connection string. 至此,您希望在JDBC连接字符串中使用useUnicode=truecharacterEncoding=UTF-8属性。 Eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM