找不到元素'mvc:annotation-driven'的声明

我需要从控制器返回JSON / XML数据。从我发现的结果来看,我需要@ResponseBody在我的方法中<mvc:annotation-

driven>启用它。我已经尝试过各种RnD,但仍然遇到问题!:(

显然我的问题出在我的servlet.xml文件中(该模式未得到验证!),我正在使用Spring 3.1.1,并且已在我的类路径中明确放入spring-

mvc-3.1.1.jar。

这是我的servlet上下文文件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>

我的控制器类如下所示:

@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:匹配的通配符是严格的,但是找不到元素“ mvc:annotation-driven”的声明。

而且,我已经尝试了本网站及其他网站上建议的所有解决方法。使用Spring 3.1.1和更新了我的架构名称空间@ResponseBody

回答:

如错误所示,架构声明有问题。您没有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">

以上是 找不到元素&#39;mvc:annotation-driven&#39;的声明 的全部内容, 来源链接: utcz.com/qa/417035.html

回到顶部