SpringMVC的亂碼處理,SpringMVC亂碼處理

來源:互聯網
上載者:User

SpringMVC的亂碼處理,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/

    所以每種問題的解決都受其環境局限,沒有什麼技術含量,記下來萬一能幫上其他網友呢。

      

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.