”通配符的匹配很全面, 但无法找到元素 '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">

其实,应该这样配置:

<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">

同时,应该加入以下配置:

<!--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 />

这里的变化是:

  • 添加<mvc:annotation-driven />,否则已经添加的注解,不起作用
  • 其次使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter适配器,而不是原先的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter适配器,大概是从SpringMvc3.1开始改变的

转载于:https://my.oschina.net/u/3268478/blog/1528542

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值