2013-03-14 61 views
17

私のコントローラからJSON/XMLデータを返す必要があります。私が見つけたものから、@ResponseBodyが私のメソッドに必要で、そのためには<mvc:annotation-driven>が有効です。私はあらゆる種類のRnDを試しましたが、まだまだ立ち往生しています! :(要素 'mvc:annotation-driven'の宣言が見つかりません

どうやら私の問題は私のservlet.xmlファイル(スキーマイマイチが有効になっ!) にあり、私は春の3.1.1 &を使用していますが、明示的に私のクラスパスに春-MVC-3.1.1.jarに入れています。

ここに私のサーブレット・コンテキストファイルのサンプル-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&validateのローカルコピーを保持する方法はありますか? – user2164016

+4

これは実際に働いたインターネット全体の唯一の答えでした。インターネットは非常に大きな場所です。 –

+0

ありがとうございました。 –

関連する問題