2013-03-14 13 views
17

Ho il requisito di restituire dati JSON/XML dal mio controller. Da quello che ho trovato, avevo bisogno di @ResponseBody nel mio metodo e per quello ho bisogno di <mvc:annotation-driven> abilitato. Ho provato tutti i tipi di RnD ma sono ancora bloccato! :(non è possibile trovare alcuna dichiarazione per l'elemento 'mvc: annotation-driven'

A quanto pare il mio problema sta nel mio file servlet.xml (il depliant schema di ottenere convalidato!) Sto usando Primavera 3.1.1 & hanno esplicitamente messo in primavera-MVC-3.1.1.jar nel mio percorso di classe.

Ecco la mia servlet-contesto file di esempio-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> 

mia classe controller si presenta così:

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

Ora il problema è <mvc:annotation-driven /> dà errore:

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.

e ho provato tutte le soluzioni alternative suggerite su questo sito e non solo. Ho aggiornato gli spazi dei nomi dello schema, utilizzando Spring 3.1.1 e @ResponseBody.

risposta

47

Come suggerisce l'errore, c'è qualcosa di sbagliato nella dichiarazione dello schema. Non hai dichiarato il xsd s. Usa questo invece.

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

Sì! Era come se avessi indovinato ... grazie per l'XSD corretto :) Anche questo xsd è convalidato tramite un url, c'è un modo in cui posso mantenere una copia locale di questi xsd e validare da lì? – user2164016

+4

Questa è stata l'unica risposta su Internet che ha funzionato, e Internet è un posto molto grande :) –

+0

Grazie per aver fatto la mia giornata –

Problemi correlati