1. Import the relevant jar package: Jackson-annotation-2.5.4.jar,jackson-core-2.5.4.jar,jackson-databind-2.5.4.jar.
Related configurations in 2.spring-servlet.xml:
Namespaces added in mvc:xmlns:mvc= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC"//xsi:schemalocation:/http Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd
<!--springmvc4.0 Configuration Method--
<!--start the annotation-driven MVC feature -<Mvc:annotation-drivenContent-negotiation-manager= "Contentnegotiationmanager"/> <Context:annotation-config/><BeanID= "Contentnegotiationmanager"class= "Org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> < Propertyname= "Favorpathextension"value= "false"></ Property></Bean><BeanID= "Jsonconverter"class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></Bean><BeanID= "Stringconverter"class= "Org.springframework.http.converter.StringHttpMessageConverter"> < Propertyname= "Supportedmediatypes"> <List> <value>Text/plain;charset=utf-8</value> </List> </ Property></Bean><Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> < Propertyname= "Messageconverters"> <List> <refBean= "Jsonconverter"/> <refBean= "Stringconverter"/> </List> </ Property></Bean>
3. Edit the Code
Click Btn, trigger Ajax, call the Controller method, return the JSON format data, and display it in the page
3.1 JSP code Form section:
<DivID= "ShowData">${MSG}</Div><formID= "Paging">page:<inputtype= "text"name= "page"ID= "page"/>rows:<inputtype= "text"name= "Rows"ID= "Rows"/> <inputID= "Sub"type= "button"value= "Submit"/> </form>
3.2 JS Code Ajax section:
$ (' #sub '). Click (function() {alert ("111"); $.ajax ({type:"POST", URL:"Syslogpaging.do", data:{"Page": $ ("#page"). Val (),"Rows": $ ("#rows"). Val ()}, DataType:"JSON", Success:function(data) {varDataajax=json.stringify (data);//Convert the JSON object data to a string type so that the page output$ ("#showData"). HTML (Dataajax). Show (); }, Error:function() {alert (Error); } }); });
3.3 Java controller is called part of the code:
@RequestMapping (value= "/syslogpaging", Method=requestmethod.post)// paging Call function, return type is Jsonobject @ResponseBody // must write, mark the transfer stream data Public jsonobject getsystemlog_paging (String page,string rows) { new// The custom ToJson class, which is used to stitch the JSON data in a specific format required by the project to return Syslogjson.tojson (sysloglist,page,rows); }
Summary: 1. The required jar packages must be imported, with regard to configuration, springmvc4.0 and 3.0 are different and need attention.
2. If the transfer stream data (format String,json) controller @ResponseBody cannot be omitted.
3. To return the JSON object data obtained by Ajax to the corresponding position of the page, adjust json.stringify (); Function converts object to string and then show ().
springmvc4.0 Configuring AJAX Request JSON Format data