The first time to write a blog is not good, but hope to help everyone, there is no biased place to hope that we treatise. I've been bothering you for two days on this issue, and I can't sleep for two days. Have been thinking about this question. Not much nonsense to say below to get to the chase.
1. Create a Web project, add Springmvc jar, I demonstrate here with spring-framework-4.2.3.release. The jar package looks like this:
2. Configure the Web. Xml as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3. Configure Applicationcontext.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/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd ">
<context:component-scan base-package= "Cn.xugang ..." ></context:component-scan>
<mvc:annotation-driven content-negotiation-manager= "Contentnegotiationmanagerfactorybean"/>
<mvc:default-servlet-handler/>
<bean class= "Org.springframework.web.accept.ContentNegotiationManagerFactoryBean" id= " Contentnegotiationmanagerfactorybean ">
<property name= "Favorpathextension" value= "false"/>
<property name= "Favorparameter" value= "false"/>
<property name= "Ignoreacceptheader" value= "false"/>
<property name= "MediaTypes" >
<map>
<entry key= "xml" value= "Application/xml"/>
<entry key= "JSON" value= "Application/json"/>
<entry key= "xls" value= "application/vnd.ms-excel"/>
</map>
</property>
</bean>
<bean class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" id= " Mappingjacksonhttpmessageconverter ">
<property name= "Supportedmediatypes" >
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id= "Annotationmethodhandleradapter" class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter ">
<property name= "Messageconverters" >
<list>
<ref bean= "Mappingjacksonhttpmessageconverter"/>
</list>
</property>
</bean>
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/web-inf/jsp/" ></property>
<property name= "suffix" value= ". JSP" ></property>
</bean>
</beans>
4. Configure create controller, use @responsebody annotations, specify what to return.
Package Cn.xugang.cotroller;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.ResponseBody;
Import Cn.xugang.entity.User;
@Controller
@RequestMapping ("/user")
public class Usercotroller {
@RequestMapping (value= "/hello.html", Method=requestmethod.get)
Public @ResponseBody User Hello () {
User User=new user ();
User.setage (19);
User.setname ("test");
return user;
}
@RequestMapping (value= "/test.html")
Public String test () {
System.out.println ("Hello World");
return "Hello";
}
}
5. Send an asynchronous request. Because I used the etx.js, I sent the asynchronous request directly with Ext.js.
Ext.onready (function () {
Ext.define (' User ', {
Extend: ' Ext.data.Model ',
fields:[' name ', ' age ',
});
var store=ext.create (' Ext.data.Store ', {
Model: ' User ',
proxy:{
Type: ' Ajax ',
URL: '/springmvc4/user/hello.html ',
reader:{
Type: ' JSON ',
}
}
});
Store.load ({
Callback:function (records,operation,success) {
if (success) {
var msg=[];
Store.each (function (user) {
Msg.push (User.get (' name ') + "" +user.get (' age '));
});
Ext.MessageBox.alert ("Hint", Msg.join ("<br>"));
}
}
});
var msg=[];
Store.each (function (user) {
Msg.push (Person.get (' name ') + "" +user.get (' age '));
});
Ext.MessageBox.alert (' Hint ', Msg.join ("<br>"));
});
6. Results presentation
7. Summary: Finally, simply say, SpringMVC4.0 above in this way to successfully return JSON, if it is less than 4.0, because of the JSON parsing different, here is a pit, in the online received a lot of information, see said is foggy, completely unable to use, finally through their own carefully summed up the contents of this article, 4.0 below can use the following JA R package.
and configure the following code in Applicationcontext.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/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd ">
<context:component-scan base-package= "Cn.xugang ..." ></context:component-scan>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" id= " Mappingjacksonhttpmessageconverter ">
<property name= "Supportedmediatypes" >
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/web-inf/jsp/" ></property>
<property name= "suffix" value= ". JSP" ></property>
</bean>
</beans>
Finally, I hope that the post is useful to everyone, because the first write, a lot of places are not very good, please forgive us. If you have doubts about the above, you can leave a message. Please specify the source if reproduced. Thank you!
SpringMVC4.0 later versions return JSON format data problems