找不到元素“mvc:annotation-driven"的声明 [英] no declaration can be found for element 'mvc:annotation-driven'
问题描述
我需要从控制器返回 JSON/XML 数据.根据我的发现,我的方法中需要 @ResponseBody
,为此我需要 <mvc:annotation-驱动>
启用.我尝试了各种 RnD,但仍然卡住了!:(
I have the requirement of returning JSON/XML data from my controller.From what i found,I needed @ResponseBody
in my method and for that I need <mvc:annotation-driven>
enabled. I have tried all sorts of RnD but am still stuck! :(
显然我的问题在于我的 servlet.xml 文件(架构没有得到验证!)我正在使用 Spring 3.1.1 &已明确将 spring-mvc-3.1.1.jar 放入我的类路径中.
Apparently my problem lies in my servlet.xml file (the schema isnt getting validated!) I am using Spring 3.1.1 & have explicitly put in spring-mvc-3.1.1.jar in my classpath.
这是我的 servlet 上下文文件 sample-servlet.xml:
Here's my servlet-context file 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>
我的控制器类看起来像这样:
My controller class looks like this :
@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;
}
}
现在的问题是
报错:
cvc-complex-type.2.4.c:匹配的通配符是严格的,但是找不到元素'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
更新了我的架构命名空间.
And I have tried all the workarounds suggested on this site and beyond. Have updated my schema namespaces, using Spring 3.1.1 and @ResponseBody
.
推荐答案
由于错误表明架构声明有问题.您没有声明 xsd
.改用这个.
As the error suggests there's something wrong with the schema declaration. You do not have the xsd
s declared.
Use this instead.
<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">
这篇关于找不到元素“mvc:annotation-driven"的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!