posts - 495,comments - 227,trackbacks - 0
          http://wxynxyo.iteye.com/blog/2000327

          使用spingmvc,在JS里面通過ajax發(fā)送請求,并返回json格式的數(shù)據(jù),從數(shù)據(jù)庫拿出來是正確的中文格式,展示在頁面上就是錯誤的??,研究了一下,有幾種解決辦法。

            我使用的是sping-web-3.2.2,jar

            方法一:

            在@RequestMapping里面加入produces = "text/html;charset=UTF-8"
          Java代碼  收藏代碼
          1. @RequestMapping(value = "/configrole", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")  
          2. public @ResponseBody String configrole() {  
          3.   ......  
          4. }  


          方法二:

          因?yàn)樵赟tringHttpMessageConverter里面默認(rèn)設(shè)置了字符集是ISO-8859-1

          所以拿到源代碼,修改成UTF-8并打包到spring-web-3.2.2.jar

          Java代碼  收藏代碼
          1. public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String>  
          2. {  
          3.   public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");  
          4.   ..........  
          5. }  
          6.    


          方法三:

          修改org.springframework.http.MediaType它的構(gòu)造方法的參數(shù),并在applicationContext-mvc.xml 加入配置
          Java代碼  收藏代碼
          1. public MediaType(String type, String subtype, Charset charset) {  
          2.     super(type, subtype, charset);  
          3. }  


          Xml代碼  收藏代碼
          1. <bean id="stringHttpMessageConverter"  
          2.     class="org.springframework.http.converter.StringHttpMessageConverter">  
          3.     <property name="supportedMediaTypes">  
          4.         <list>  
          5.             <bean class="org.springframework.http.MediaType">  
          6.                 <constructor-arg value="text" />  
          7.                 <constructor-arg value="plain" />  
          8.                 <constructor-arg value="UTF-8" />  
          9.             </bean>  
          10.         </list>  
          11.     </property>  
          12. </bean>  


          方法四:

            直接將org.springframework.http.converter.StringHttpMessageConverter 里面的屬性defaultCharset設(shè)置成utf-8
          Xml代碼  收藏代碼
          1. <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">  
          2. <constructor-arg value="UTF-8" />
          3. </bean>  

          posted on 2015-04-09 17:05 SIMONE 閱讀(8536) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 高安市| 贡觉县| 东宁县| 寿宁县| 孝昌县| 曲沃县| 电白县| 昌江| 鹤岗市| 茌平县| 连平县| 高雄市| 湟源县| SHOW| 同仁县| 偏关县| 明水县| 怀仁县| 乌兰浩特市| 开平市| 万载县| 内丘县| 建水县| 平湖市| 清河县| 象州县| 浮梁县| 偏关县| 东莞市| 乌审旗| 阳信县| 越西县| 定兴县| 滨州市| 四川省| 玛沁县| 门源| 交口县| 讷河市| 沅陵县| 徐闻县|