问题描述
我从controller.From返回JSON / XML数据,我需要 @ResponseBody
在我的方法和什么我发现我需要的要求<$ C 启用;注解驱动&GT:$ C>&LT MVC。我已经试过各种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和放大器;已经明确提出春天-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上下文文件样本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;
}
}
现在的问题是&LT; MVC:注解驱动/&GT;
给出了错误:
Now the problem is <mvc:annotation-driven />
gives error:
CVC-复杂type.2.4.c:匹配通配符是严格的,但没有声明可以为元素被发现。MVC:注解驱动
我试图在这个网站,并建议以后所有的解决方法。已经更新我的架构命名空间,使用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:注解驱动“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!