国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

SSM框架——SpringMVC中使用@ResponseBody注解返回值,Ajax取得中文亂碼解決方法

2019-11-10 20:02:52
字體:
來源:轉載
供稿:網友
SPRing使用AnnotationMethodHandlerAdapter的handleResponseBody方法, AnnotationMethodHandlerAdapter使用request header中"Accept"的值和messageConverter支持的MediaType進行匹配,然后會用"Accept"的第一個值寫入 response的"Content-Type"。一般的請求都是通過瀏覽器進行的,request header中"Accept"的值由瀏覽器生成。  

有人跟蹤@ResponseBody 的實現類發現其默認的編碼是 iso-8859-1,所以顯然Ajax接受服務器端返回的中文必然是亂碼。

下面提供2中解決方法:

方法一

我遇到這個問題的時候,查閱了一下資料,采用了一個比較簡單的方法來解決這個問題,就是需要服務器返回中文的時候不使用這個注解,而是直接用HttpServletResponse的對象來完成傳輸,在服務器端可以通過response.setContentType("text/plain;charset=UTF-8");來設定編碼類型,這樣就不會出現中文亂碼了。

服務器端核心代碼如下:

[java] view plain copy print?@RequestMapping(value = "test", method = RequestMethod.POST)      public void test(HttpServletRequest request,              HttpServletResponse response) {          String result = null;          //取得客戶端傳來的值          String userName = request.getParameter("userName");          //向客戶端返回一句話          result = "您好!";            PrintWriter out = null;          response.setContentType("text/plain;charset=UTF-8");          try {              out = response.getWriter();              out.write(result.toString());          } catch (IOException e) {              e.printStackTrace();          } finally {              out.close();          }      }  返回值時根據自己的數據類型進行設置,常用的有:

response.setContentType("text/html; charset=utf-8");           htmlresponse.setContentType("text/plain; charset=utf-8");          文本response.setContentType("application/json; charset=utf-8");    數據response.setContentType("application/xml; charset=utf-8");      xml

方法二

2014-07-11

今天再次查找了一下這個問題,有了一個更好的解決方法,使用spring的BeanPostProcessor接口實現,在自己的工程中新建一個類,如下:

[java] view plain copy print?package springmvc.extention;    import java.nio.charset.Charset;  import java.util.ArrayList;  import java.util.List;    import org.springframework.beans.BeansException;  import org.springframework.beans.factory.config.BeanPostProcessor;  import org.springframework.http.MediaType;  import org.springframework.http.converter.StringHttpMessageConverter;    /**   * 解決spring MVC3 中@ResponseBody的中文亂碼問題   */    public class UTF8StringBeanPostProcessor implements BeanPostProcessor {        @Override        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {            if (bean instanceof StringHttpMessageConverter) {                MediaType mediaType = new MediaType("text", "plain", Charset.forName("UTF-8"));                List<MediaType> types = new ArrayList<MediaType>();                types.add(mediaType);                ((StringHttpMessageConverter) bean).setSupportedMediaTypes(types);            }            return bean;        }            @Override        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {            return bean;        }    }  然后在自己的Spring配置文件中注冊這個bean就可以了,再試試自己的程序,發現問題解決了。

[html] view plain copy print?<!-- 解決使用@ResponseBody 的中文亂碼。 -->      <bean class="springmvc.extention.UTF8StringBeanPostProcessor"></bean>  

(原文地址:http://blog.csdn.NET/zhshulin)


上一篇:Leetcode 198. House Robber

下一篇:文章標題

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 疏附县| 历史| 婺源县| 北海市| 手机| 会东县| 竹北市| 莒南县| 丰都县| 濉溪县| 舟曲县| 象州县| 和政县| 互助| 乾安县| 苍山县| 永川市| 南宫市| 庆安县| 松溪县| 虎林市| 清镇市| 枣强县| 伊春市| 内黄县| 新建县| 莱阳市| 嘉祥县| 井陉县| 灌阳县| 电白县| 谢通门县| 营口市| 闻喜县| 都兰县| 西宁市| 牙克石市| 当阳市| 桂林市| 尚志市| 龙泉市|