ITeye博客

spring3 MVC国际化支持之中文乱码

juyon · 2010-06-28 21:40 · spring3.x · 企业架构 · Markdown

最近时间在研究spring3的mvc编程。话不多说,直接上代码: 一、页面编码统一采用utf-8(拥抱国际标准)

<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

二、过滤器编码也为utf-8

<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<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>

	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

三、甚至连.properties的编码方式都改成了utf-8:

设置完成,但页面显示为乱码。 后来分析了一下源码:增加了"defaultEncoding"属性为"UTF-8":

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

利用utf-8显示中文问题终于解决了. 注:受struts影响,还使用了native2ascii工具转换编码,还是不行。google,baidu了很久都没找到解决方法。

评论(2)

  1. #2 楼 · 乐乎其中 · 2013-07-15 16:24

    我开始也是这样,用了native2ascii转了,一直是乱码,后来不转换直接写中文,竟然奇迹般的没出现乱码(我的properties文件采用的是utf-8编码,页面也是),我也不知道怎么回事,你可以试试。

  2. #1 楼 · 叫我Fox · 2011-05-27 10:06

    找到解决的办法了么 我也遇到了同样的问题

评论功能维护中,当前仅展示历史评论。