use*_*872 13 spring tomcat character-encoding
我在网站上遇到编码的大问题!我使用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">
Run Code Online (Sandbox Code Playgroud)
我创建了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>
Run Code Online (Sandbox Code Playgroud)
并将以下内容添加到我的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" />
Run Code Online (Sandbox Code Playgroud)
我在server.xml的元素中将useBodyEncodingForURI属性设置为true:%CATALINA_HOME%/ conf,另一次尝试添加URIEncoding ="UTF-8".
我使用charset [utf8]和集合[utf8_general_ci]创建了所有表和字段
我的浏览器中的编码是UTF-8(BTW,我有IE8和Firefox 3.6.3)
当我打开MYSQL查询浏览器并手动插入捷克语或德语数据时,它正确插入,并在我的应用程序中正确显示.
所以,这是我遇到的问题清单:
默认情况下,应加载messages.properties(Czech),而默认情况下会加载messages_en.properties.
在Web表单中,当我输入捷克数据,然后单击提交,在控制器中我打印出控制台中的数据,然后将其保存到数据库,打印的内容不正确有奇怪的字符,这是确切的数据,保存到db.
我不知道哪里出错了!为什么我不能让它工作,虽然我做了人们为他们所做的工作!不知道..
请帮助我,我几天都陷入这个糟糕的问题,它让我发疯!
先感谢您.
Rob*_*ell 15
首先,如果您的项目使用Maven,请确保Maven Resources Plugin将UTF-8设置为其字符编码方案,否则可能会使用不正确的编码将消息属性文件写入目标.
其次,您正在使用ResourceBundleMessageSource,它使用标准的java.util.ResourceBundle和java.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>
Run Code Online (Sandbox Code Playgroud)
我在Cake Solutions博客文章中发现了这一点.
| 归档时间: |
|
| 查看次数: |
16672 次 |
| 最近记录: |