這篇文章主要介紹了如何解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼的問題,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1、使用ajax從前臺(tái)頁面?zhèn)鬏敂?shù)據(jù)到后臺(tái)controller控制器的時(shí)候,出現(xiàn)中文亂碼(問號(hào)???)。
之前在網(wǎng)上找了各種解決方案,都行不通,最后發(fā)現(xiàn)是tomcat服務(wù)器接收數(shù)據(jù)的問題
解決方案:
方式一:在controller接收參數(shù)時(shí),對(duì)參數(shù)進(jìn)行轉(zhuǎn)碼
@ResponseBody
@RequestMapping(value="/getJsonDataByCityName",produces="application/json")
public String getJsonByName(HttpServletRequest request,HttpServletResponse response,@RequestParam String city_name)throws ServletException,IOException
{
//response.setContentType("text/html;charset=UTF-8");
//request.setCharacterEncoding("UTF-8");//解決post亂碼問題
System.out.println(request.getCharacterEncoding());
city_name = new String(city_name.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("city_name:"+city_name);
}方式二:
配置tomcat目錄下的service.xml文件
tomcat7/conf/server.xml
給該行代碼加上 URIEncoding="UTF-8" 的編碼屬性
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
原因分析:
其實(shí)亂碼問題出現(xiàn)的原因,就是由于默認(rèn)的tomcat配置,接收請(qǐng)求是以ISO-8859-1來轉(zhuǎn)碼,導(dǎo)致中文出現(xiàn)了亂碼問題,只要能正確的以u(píng)tf-8來轉(zhuǎn)碼,則可以解決亂碼問題。
2、普通數(shù)據(jù)傳輸,從jsp頁面?zhèn)鞯胶笈_(tái)controller,中文亂碼問題解決方案
(1)、首先檢查jsp頁面編碼格式是否是utf-8
(2)、設(shè)置中文過濾
<!-- 中文編碼 --> <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>
(3)、springMvc配置文件中設(shè)置JSON數(shù)據(jù)轉(zhuǎn)換
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- 啟動(dòng)Spring MVC的注解功能,完成請(qǐng)求和注解POJO的映射 注解請(qǐng)求映射 默認(rèn)是ISO-88859-1,避免亂碼這里設(shè)置為UTF-8 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> </bean> <!-- 啟動(dòng)JSON格式的配置,自動(dòng)將格式轉(zhuǎn)換成JSON格式,不需要其他類 --> <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼的問題”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
本文題目:如何解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼的問題-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://chinadenli.net/article2/dhsgoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、服務(wù)器托管、網(wǎng)站策劃、網(wǎng)頁設(shè)計(jì)公司、全網(wǎng)營銷推廣、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容