字符编码问题春天 [英] Character-encoding problem spring

查看:210
本文介绍了字符编码问题春天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个大问题,在我的网站编码!
我使用spring 3,tomcat 6和mysql db。我想在我的网站上支持德语和捷克语以及英语,我创建了所有的JSP作为UTF-8文件,在每个jsp我包括以下:

 <%@ page language =javacontentType =text / html; charset = UTF-8
pageEncoding =UTF-8%&
< meta http-equiv =Content-Typecontent =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/> ;

<! - 声明拦截器 - >
< mvc:interceptors>
< bean class =org.springframework.web.servlet.i18n.LocaleChangeInterceptor
p:paramName =locale/>
< / mvc:interceptors>

<! - 声明解析器 - >
< bean id =localeResolver
class =org.springframework.web.servlet.i18n.SessionLocaleResolver/>

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



我使用charset [utf8]和collection [utf8_general_ci]创建了所有的表和字段。 p>

我的浏览器中的编码是UTF-8(BTW,我有IE8和Firefox 3.6.3)



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



这里是问题列表我有:


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


  2. 在网络表单中,当我输入捷克语的数据,然后点击提交,在控制器中打印输出数据在控制台之前,正在打印的是不正确的,有奇怪的字符,这是保存到db的确切数据。


不知道哪里有错误!为什么我不能得到它的工作,虽然我做了人们做了和为他们工作!不知道..



请帮助我,自从几天以来,我一直陷入这个疯狂的问题,它驱使我疯了!



提前感谢。

解决方案

首先,如果你的项目使用Maven, a href =http://maven.apache.org/plugins/maven-resources-plugin/examples/encoding.html =nofollow noreferrer> Maven Resources Plugin的UTF-8设置为其字符编码方案< a>,否则消息属性文件可能会使用不正确的编码写入目标。



其次,您使用的是 ResourceBundleMessageSource 使用标准的 java.util.ResourceBundle java.util.Properties ,它只支持ISO-8859-1编码。您可以改用 ReloadableResourceBundleMessageSource

 < bean id =messageSourceclass = .springframework.context.support.ReloadableResourceBundleMessageSource> 
< property name =basenamevalue =classpath:messages/>
< property name =defaultEncodingvalue =UTF-8/>
< / bean>

我发现从 Cake Solutions博客文章。


I am stuck in a big problem with encoding in my website! I use spring 3, tomcat 6, and 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:

<%@ 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. And all of them are saved as UTF-8 files.

I added the following to 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:

<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.

I created all the tables and fields with charset [utf8] and collection [utf8_general_ci]

The encoding in my browser is UTF-8 (BTW, I have IE8 and 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.

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.

  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.

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.

Second, you're using ResourceBundleMessageSource, which uses the standard java.util.ResourceBundle and java.util.Properties, which only support ISO-8859-1 encoding. You can instead use ReloadableResourceBundleMessageSource like:

<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.

这篇关于字符编码问题春天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆