【问题标题】:no declaration can be found for element 'mvc:annotation-driven'找不到元素“mvc:annotation-driven”的声明
【发布时间】:2013-03-02 14:56:26
【问题描述】:

我需要从我的控制器返回 JSON/XML 数据。根据我的发现,我需要在我的方法中使用 @ResponseBody,为此我需要启用 <mvc:annotation-driven>。我尝试了各种 RnD,但仍然卡住了! :(

显然我的问题在于我的 servlet.xml 文件(架构没有得到验证!) 我正在使用 Spring 3.1.1 并在我的类路径中明确放入 spring-mvc-3.1.1.jar。

这是我的 servlet-context 文件 sample-servlet.xml:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/tx                      http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/mvc-3.1  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
         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">


  <context:component-scan base-package="com.sample.controller"/>
  <mvc:annotation-driven/>  

  <!--Use JAXB OXM marshaller to marshall/unmarshall following class-->
  <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

  <bean id="xmlViewer" 
        class="org.springframework.web.servlet.view.xml.MarshallingView">
    <constructor-arg>
      <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
          <list>
            <value>com.sample.model.SampleClass</value>
          </list>
        </property>
      </bean>
    </constructor-arg>
  </bean>

  <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
</bean> 

我的控制器类如下所示:

@Controller
public class XmlController {

  @RequestMapping(value="/getXml",method = RequestMethod.POST)
  public @ResponseBody AssociateDetail getXml(){
    System.out.println("inside xml controller.....");
    AssociateDetail assoBean=null;

    try{
      AssociateService add=new AssociateService();
      assoBean=add.selectAssociateBean();
    }catch(Exception e){
      e.printStackTrace();
    }

    return assoBean;    
  }
}

现在的问题是&lt;mvc:annotation-driven /&gt; 给出错误:

cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素'mvc:annotation-driven'的声明。

我已经尝试了本网站及其他网站上建议的所有解决方法。使用 Spring 3.1.1 和 @ResponseBody 更新了我的模式命名空间。

【问题讨论】:

    标签: java xml spring-mvc annotations spring-3


    【解决方案1】:

    正如错误表明架构声明有问题。您没有声明 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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    

    【讨论】:

    • 是的!就像我猜到了..感谢正确的 XSD :) 此 xsd 也是通过 url 验证的,有什么方法可以保留这些 xsd 的本地副本并从那里验证?
    • 这是整个互联网上唯一有效的答案,互联网是一个非常大的地方:)
    • 它表示您不需要在 xsd (spring-mvc-3.1.xsd) 中指定版本。如果没有给出版本,Spring 将在项目的依赖项中使用最高版本。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 1970-01-01
      • 2018-04-12
      • 2014-11-18
      • 1970-01-01
      • 2011-08-28
      • 2012-07-02
      • 2012-11-28
      • 2012-09-14
      相关资源
      最近更新 更多