web-dev-qa-db-fra.com

aucune déclaration n'a pu être trouvée pour l'élément 'mvc: annotation-driven'

J'ai l'obligation de retourner les données JSON/XML de mon contrôleur. D'après ce que j'ai trouvé, j'avais besoin de @ResponseBody dans ma méthode et pour cela j'ai besoin de <mvc:annotation-driven> activée. J'ai essayé toutes sortes de RnD mais je suis toujours coincé! :(

Apparemment, mon problème réside dans mon fichier servlet.xml (le schéma n'est pas validé!) J'utilise Spring 3.1.1 et l'ai explicitement mis dans spring-mvc-3.1.1.jar dans mon chemin de classe.

Voici mon fichier de contexte de servlet 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> 

Ma classe de contrôleur ressemble à ceci:

@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;    
  }
}

Maintenant, le problème est <mvc:annotation-driven /> donne une erreur:

cvc-complex-type.2.4.c: le caractère générique correspondant est strict, mais aucune déclaration ne peut être trouvée pour l'élément 'mvc: annotation-driven'.

Et j'ai essayé toutes les solutions de contournement suggérées sur ce site et au-delà. J'ai mis à jour mes espaces de noms de schéma, en utilisant Spring 3.1.1 et @ResponseBody.

20
user2164016

Comme l'erreur l'indique, la déclaration de schéma présente un problème. Vous n'avez pas déclaré les xsd s. Utilisez-le à la place.

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

Ajoutez les schémas suivants à votre déclaration schemaLocation en haut:

http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

2
VHS