首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >找不到“mvc:annotation-driven”元素的声明

找不到“mvc:annotation-driven”元素的声明
EN

Stack Overflow用户
提问于 2013-03-14 18:06:07
回答 2查看 43.9K关注 0票数 22

我有从我的controller.From返回JSON/XML数据的需求,我需要在我的方法中使用@ResponseBody,为此我需要启用<mvc:annotation-driven>。我试过各种RnD,但还是卡住了!

显然,我的问题出在我的servlet.xml文件中(模式没有得到验证!)我使用的是Spring 3.1.1,并在类路径中显式地放入了spring-mvc-3.1.1.jar。

下面是我的servlet上下文文件sample-servlet.xml:

代码语言:javascript
复制
<?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> 

我的控制器类如下所示:

代码语言:javascript
复制
@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;    
  }
}

现在的问题是<mvc:annotation-driven />给出了错误:

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

我已经尝试了这个网站和其他网站上建议的所有变通方法。我使用Spring3.1.1和@ResponseBody更新了我的模式命名空间。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-15 00:24:09

正如错误所示,模式声明有问题。您没有声明xsd %s。改用这个。

代码语言:javascript
复制
<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">
票数 56
EN

Stack Overflow用户

发布于 2016-02-11 01:15:51

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15406231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档