2013-03-14 42 views
17

我有要求從我的控制器返回JSON/XML數據。從我發現的情況來看,我需要@ResponseBody在我的方法中,並且我需要啓用<mvc:annotation-driven>。我已經嘗試了各種RnD,但我仍然堅持! :(對元素'mvc:annotation-driven'沒有聲明

顯然我的問題在於我servlet.xml文件(架構心不是得到驗證!) 我使用Spring 3.1.1 &已經明確提出在彈簧MVC-3.1.1.jar在我的類路徑。

這裏是我的servlet上下文的文件樣本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;  
    } 
} 

現在的問題是<mvc:annotation-driven />給出了錯誤:

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

我試圖在本網站和超越建議的所有解決方法。已經使用Spring 3.1.1和@ResponseBody更新了我的模式命名空間。

回答

47

由於錯誤提示架構聲明出了問題。您沒有聲明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"> 
+0

是的!這就像我已經猜到了..謝謝你正確的XSD :) 此外這個xsd通過一個url驗證,有沒有什麼辦法可以保留這些xsd的本地副本&從那裏驗證? – user2164016 2013-03-19 14:16:39

+4

這是在整個互聯網上唯一的答案,實際上工作,互聯網是一個非常大的地方:) – 2014-04-23 19:33:31

+0

謝謝你讓我的一天 – 2015-02-03 09:44:09