最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問(wèn)題的幾種方法

 更新時(shí)間:2018年01月29日 15:05:43   作者:摯愛冷如煙°  
本篇文章主要介紹了解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問(wèn)題的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1、使用ajax從前臺(tái)頁(yè)面?zhèn)鬏敂?shù)據(jù)到后臺(tái)controller控制器的時(shí)候,出現(xiàn)中文亂碼(問(wèn)號(hào)???)。

之前在網(wǎng)上找了各種解決方案,都行不通,最后發(fā)現(xiàn)是tomcat服務(wù)器接收數(shù)據(jù)的問(wèn)題

解決方案:

方式一:在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亂碼問(wèn)題
  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í)亂碼問(wèn)題出現(xiàn)的原因,就是由于默認(rèn)的tomcat配置,接收請(qǐng)求是以ISO-8859-1來(lái)轉(zhuǎn)碼,導(dǎo)致中文出現(xiàn)了亂碼問(wèn)題,只要能正確的以u(píng)tf-8來(lái)轉(zhuǎn)碼,則可以解決亂碼問(wèn)題。

2、普通數(shù)據(jù)傳輸,從jsp頁(yè)面?zhèn)鞯胶笈_(tái)controller,中文亂碼問(wèn)題解決方案

(1)、首先檢查jsp頁(yè)面編碼格式是否是utf-8

(2)、設(shè)置中文過(guò)濾

<!-- 中文編碼 -->
  <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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

盱眙县| 大悟县| 东城区| 绍兴市| 东阳市| 洞头县| 井研县| 石狮市| 石楼县| 出国| 锡林郭勒盟| 桃江县| 安岳县| 卢氏县| 泸西县| 长宁县| 邵武市| 隆德县| 孟村| 油尖旺区| 舒兰市| 岳普湖县| 义马市| 称多县| 得荣县| 苏尼特右旗| 克什克腾旗| 福海县| 自治县| 新密市| 乌拉特后旗| 贺州市| 永康市| 佛山市| 长治市| 梁山县| 江城| 威远县| 彭阳县| 长白| 磐石市|