概述
中國(guó)特色社會(huì)主義亂碼問(wèn)題是我們經(jīng)常會(huì)碰到的問(wèn)題,解決的辦法有很多,本文分別介紹了GET方式和POST方式中文亂碼解決方案中一勞永逸的辦法。
GET提交中文亂碼解決方案
在亂碼的Controller文件中采用下面的方法將編碼轉(zhuǎn)換成UTF-8
1
|
String str = new String(request.getParameter( "參數(shù)名" ).getBytes( "iso-8859-1" ), "utf-8" ); |
修改項(xiàng)目所在的Tomcat服務(wù)器中的server.xml文件
將
1
|
< Connector connectionTimeout = "20000" port = "8080" protocol = "HTTP/1.1" redirectPort = "8443" /> |
修改為:
1
|
< Connector URIEncoding = "UTF-8" connectionTimeout = "20000" port = "8080" protocol = "HTTP/1.1" redirectPort = "8443" /> |
對(duì)于A(yíng)jax請(qǐng)求的GET方式中文亂碼問(wèn)題用上述方法仍然能夠解決。
POST提交中文亂碼解決方案
在web.xml文件中添加下面的內(nèi)容:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- 解決POST提交中文亂碼問(wèn)題的過(guò)濾器,注意只能解決POST提交中文亂碼的問(wèn)題 --> < 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 > </ filter > < filter-mapping > < filter-name >CharacterEncodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > |
總結(jié)
以上就是本文關(guān)于Spring MVC參數(shù)傳遞中文亂碼解決方法分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/jpzhu16/article/details/54880450#post提交中文亂碼解決方案