新人學習springMVC開發(fā)框架,用到ajax 通過 @response 來獲取返回值。
成都創(chuàng)新互聯(lián)公司一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個客戶多一個朋友!為您提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、成都網(wǎng)頁設(shè)計、重慶小程序開發(fā)公司、成都網(wǎng)站開發(fā)、成都網(wǎng)站制作、成都軟件開發(fā)、app開發(fā)定制是成都本地專業(yè)的網(wǎng)站建設(shè)和網(wǎng)站設(shè)計公司,等你一起來見證!不得不說 @response的功能很強大,可以直接將返回類打包成json格式省卻了很多事,
但是如果返回值是String類型的話,就會出現(xiàn)中文亂碼問題,自己試著做了一些調(diào)整,并在網(wǎng)上查看了許多方法,在這里總結(jié)一下。
1.添加注解 produces = {"application/json;charset=UTF-8"}
@RequestMapping(value = "/method.do", produces = {"application/json;charset=UTF-8"})
適用于少量的,每寫一個方法就得添加一次,不適合統(tǒng)一處理。
2.添加配置 在springMVC-*.xml里面進行String編碼配置,如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- utf-8編碼 --> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> --> 。。。。。 </beans>
如果可以看org.springframework.http.converter.StringHttpMessageConverter這個類的源碼的話就會發(fā)現(xiàn)其默認的編碼方式為 "ISO-8859-1",
這個應(yīng)該是造成我們中文亂碼的主要原因。
在這里不能不吐槽一下,好多老外寫的jar包都會出現(xiàn)中文亂碼問題,究其主要原因就是人家不用中文。。。故肯定會選擇內(nèi)存占用小的"ISO-8859-1",
啥時候才能大家都統(tǒng)一使用utf-8呀。。
當前文章:springMVC@response中文亂碼解決-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://aaarwkj.com/article38/icspp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、做網(wǎng)站、動態(tài)網(wǎng)站、網(wǎng)站維護、網(wǎng)站排名、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容