码迷,mamicode.com
首页 > 数据库 > 详细

Spring MVC+MySQL保存中文变成乱码

时间:2014-10-12 16:33:18      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   for   

环境:MySQL,Spring MVC3.2.0,jQuery v2.0.3,使用JdbcTemplate访问数据库,相当于全套Spring解决方案。

 

现象

直接使用表单POST,或者使用jQuery POST数据到服务端,在Controller中获得的中文字符串皆为乱码。到达JdbcTemplate插入或者更新数据库时,自然也是乱码。

 

解决措施

修改web.xml,增加编码过滤器,如下(注意,需要设置forceEncoding参数值为true)

<filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

 

效果

在Controller中获得的中文字符串已正常。但使用JdbcTemplate插入或者更新数据库后,依然是乱码。可判定是写入数据库时,出现编码问题。

 

解决措施

修改applicationContext.xml文件,设置MySQL访问的url为:url="jdbc:mysql://localhost:3306/xxx?useUnicode=true&amp;characterEncoding=UTF-8"

效果

写入数据库也已经正常,读取和在网页上显示正常。

 

参考

http://blog.csdn.net/xuechongyang/article/details/8283924

http://hi.baidu.com/fuzk2008/item/956351c17a47d653bcef6956

Spring MVC+MySQL保存中文变成乱码

标签:style   blog   http   color   io   os   使用   ar   for   

原文地址:http://www.cnblogs.com/ygjlch/p/4020497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!