概述
中國特色社會主義亂碼問題是我們經常會碰到的問題,解決的辦法有很多,本文分別介紹了GET方式和POST方式中文亂碼解決方案中一勞永逸的辦法。
GET提交中文亂碼解決方案
在亂碼的Controller文件中采用下面的方法將編碼轉換成UTF-8
String str = new String(request.getParameter("參數名").getBytes("iso-8859-1"), "utf-8");
修改項目所在的Tomcat服務器中的server.xml文件
將
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
修改為:
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
對于Ajax請求的GET方式中文亂碼問題用上述方法仍然能夠解決。
POST提交中文亂碼解決方案
在web.xml文件中添加下面的內容:
<!-- 解決POST提交中文亂碼問題的過濾器,注意只能解決POST提交中文亂碼的問題 --> <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>
總結
以上就是本文關于Spring MVC參數傳遞中文亂碼解決方法分享的全部內容,希望對大家有所幫助。
新聞熱點
疑難解答
圖片精選