SpringMVC的乱码处理


最近因为修改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/

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

Spring MVC+Spring3+Hibernate4开发环境搭建  

Spring MVC整合Freemarker基于注解方式  

基于注解的Spring MVC简单介绍

SpringMVC详细示例实战教程

Spring MVC 框架搭建及详解

SpringMVC 异常处理 

本文永久更新链接地址

相关内容