2013-03-14 8 views
17

Mam wymóg zwrócenia danych JSON/XML z mojego kontrolera. Z tego co znalazłem, potrzebowałem @ResponseBody w mojej metodzie i do tego potrzebuję <mvc:annotation-driven> włączone. Próbowałem różnych rodzajów RND, ale nadal tkwię! :(nie można znaleźć deklaracji dla elementu "mvc: adnotacja sterowana"

Widocznie mój problem leży w moim pliku servlet.xml (isnt schematu coraz potwierdzone!) Używam Wiosna 3.1.1 & które jawnie umieścić w wiosenno-MVC-3.1.1.jar w mojej ścieżce klasy.

Oto mój aplet-context plik 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> 

Moja klasa kontroler wygląda następująco:

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

Teraz problem jest <mvc:annotation-driven /> daje błąd:

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

i próbowałem wszystkie obejścia proponowane na tej stronie i poza nią. Zaktualizowałem moje przestrzenie nazw schematu, używając Spring 3.1.1 i @ResponseBody.

Odpowiedz

47

Błąd sugeruje, że coś jest nie tak z deklaracją schematu. Nie masz zadeklarowanych xsd s. Użyj tego zamiast tego.

<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

Tak! To było tak, jak zgadłem. Dzięki za poprawne XSD :) Również ten XSD jest sprawdzany przez URL, czy istnieje sposób, w jaki mogę zachować lokalną kopię tych XSD i potwierdzić stamtąd? – user2164016

+4

To była jedyna odpowiedź w całym Internecie, która faktycznie działała, a internet to bardzo duże miejsce :) –

+0

Dzięki, że zrobiłam mój dzień –

Powiązane problemy