」通配符的匹配很全面, 但沒法找到元素 'mvc:annotation-driven' 的聲明「錯誤的解決方法

其實這個錯誤是用於配置文件引發的,mvc.xml配置的beans的命名空間有誤,原來是這樣的:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/tool"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tool
       http://www.springframework.org/schema/tool/spring-tool.xsd">

其實,應該這樣配置:html

<beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

同時,應該加入如下配置:web

<!--json解析器配置-->
    <!--將字符串轉換爲json-->
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
         <property name="messageConverters">
             <list>
                 <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
                     <property name="supportedMediaTypes">
                         <list>
                             <!-- 這裏順序不能反,必定先寫text/html,否則ie下出現下載提示 -->
                             <value>text/html;charset=UTF-8</value>
                             <value>application/json;charset=UTF-8</value>
                             <value>text/plain;charset=UTF-8</value>
                         </list>
                     </property>
                 </bean>
                 <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                     <property name="supportedMediaTypes">
                         <list>
                             <!-- 這裏順序不能反,必定先寫text/html,否則ie下出現下載提示 -->
                             <value>text/html;charset=UTF-8</value>
                             <value>application/json;charset=UTF-8</value>
                             <value>text/plain;charset=UTF-8</value>
                         </list>
                     </property>
                 </bean>
             </list>
         </property>
     </bean>

    <!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射 -->
    <mvc:annotation-driven />

這裏的變化是:spring

  • 添加<mvc:annotation-driven />,不然已經添加的註解,不起做用
  • 其次使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter適配器,而不是原先的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter適配器,大概是從SpringMvc3.1開始改變的
相關文章
相關標籤/搜索