码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC的乱码处理

时间:2015-06-11 19:28:53      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

    最近因为修改Spring事务的原因,修改了SpringMVC的配置文件,可是最后发现使用AJAX获取后台数据是出现乱码问题。最后发现是因为SpringMVC配置文件中新增了个标签:

<mvc:annotation-driven/>引起的,因为之前在配置文件有这样的配置:

<bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
        <property name="messageConverters">  
             <list>  
                 <bean class = "org.springframework.http.converter.StringHttpMessageConverter">  
                    <property name = "supportedMediaTypes">  
                         <list>  
                             <value>text/plain;charset=UTF-8</value>  
                         </list>  
                    </property>  
                 </bean>  
             </list>  
        </property>  
    </bean> 

    如果增加上<mvc:annotation-driven/>标签,则这个配置将会失效。由于着急,没有跟源码,直接问度娘,有网友说加上如此代码问题可以解决:

     <mvc:annotation-driven>
   <mvc:message-converters>
       <bean class="org.springframework.http.converter.StringHttpMessageConverter">
           <property name="supportedMediaTypes">
               <list>
                   <value>application/json;charset=UTF-8</value>
               </list>
           </property>
       </bean>
   </mvc:message-converters>
</mvc:annotation-driven>

      这个时候编辑器报错,说annotaion-driver标签里不能包含子元素。我去,网友怎么呢蒙人呢,后来发现是因为XSD文件版本低了,于是我换成了3.2版本的,问题解决。修改XSD引入代码如:

      <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    如果你是其他版本也可以换成其他版本,地址在:http://www.springframework.org/schema/mvc/

    所以每种问题的解决都受其环境局限,没有什么技术含量,记下来万一能帮上其他网友呢。

      

SpringMVC的乱码处理

标签:

原文地址:http://blog.csdn.net/achilles12345/article/details/46459531

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!