歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> SpringMVC的亂碼處理

SpringMVC的亂碼處理

日期:2017/3/1 9:26:58   编辑:Linux編程

最近因為修改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開發環境搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm

Spring MVC整合Freemarker基於注解方式 http://www.linuxidc.com/Linux/2013-02/79660.htm

基於注解的Spring MVC簡單介紹 http://www.linuxidc.com/Linux/2012-02/54896.htm

SpringMVC詳細示例實戰教程 http://www.linuxidc.com/Linux/2015-06/118461.htm

Spring MVC 框架搭建及詳解 http://www.linuxidc.com/Linux/2012-01/52740.htm

SpringMVC 異常處理 http://www.linuxidc.com/Linux/2015-06/119049.htm

Copyright © Linux教程網 All Rights Reserved