2013-03-14 25 views
17

Tôi có yêu cầu trả lại dữ liệu JSON/XML từ bộ điều khiển của mình. Từ những gì tôi tìm thấy, tôi cần @ResponseBody theo phương pháp của mình và cho phép tôi cần kích hoạt <mvc:annotation-driven>. Tôi đã thử tất cả các loại RnD nhưng vẫn bị mắc kẹt! :(không có khai báo nào có thể được tìm thấy cho phần tử 'mvc: hướng chú giải'

Rõ ràng vấn đề của tôi nằm trong tệp servlet.xml của tôi (giản đồ không được xác thực!) Tôi đang sử dụng Spring 3.1.1 & đã đặt rõ ràng trong spring-mvc-3.1.1.jar trong classpath của tôi.

Dưới đây là servlet-bối cảnh của tôi tập mẫu-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> 

lớp điều khiển của tôi trông như thế này:

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

Bây giờ vấn đề là <mvc:annotation-driven /> cho lỗi:

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

Và tôi đã thử tất cả các cách giải quyết đề nghị trên trang web này và xa hơn nữa. Đã cập nhật không gian tên lược đồ của tôi, sử dụng Spring 3.1.1 và @ResponseBody.

Trả lời

47

Do lỗi cho thấy có điều gì đó sai với khai báo lược đồ. Bạn không có tuyên bố xsd. Sử dụng tùy chọn này thay thế.

<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

Có! Nó giống như tôi đã đoán .. Cảm ơn cho XSD chính xác :) Ngoài ra xsd này được xác nhận thông qua một url, là có cách nào tôi có thể giữ một bản sao địa phương của các xsd & xác nhận từ đó? – user2164016

+4

Đây là câu trả lời duy nhất trên toàn bộ internet thực sự hoạt động, và internet là một nơi rất lớn :) –

+0

Cảm ơn bạn đã làm ngày của tôi –

Các vấn đề liên quan