This is used in the foreground to initiate AJAX request handler, background forgery data response to the foreground,
Configure the handler information for the corresponding AJAX request as follows
1 @Controller2 public classMycontroller {3 //Processor Method4@RequestMapping (value= "/first.do", produces= "text/html;charset=utf-8")5 public voidDofirst (httpservletresponse Response)throwsioexception{6 //Forgery of data7 8map<string, userinfo> map=NewHashmap<string,userinfo>();9UserInfo u1=NewUserInfo ();TenU1.setage (12); oneU1.setname ("congratulations on employment"); a -UserInfo u2=NewUserInfo (); -U2.setage (122); theU2.setname ("smooth employment"); - -Map.put ("001", u1); -Map.put ("002", u2); + - //tool Map----json String Fastjson +String jsonstring =json.tojsonstring (map); aResponse.setcharacterencoding ("utf-8"); at //Response Flow - response.getwriter (). Write (jsonstring); - response.getwriter (). Close (); - - } - in -}
The Fastjson tool is used here to convert the map object to a JSON string, with the response parameter passed to the Foreground.
The JSON is similar to: {"002": {"age": 122, "name": "smooth employment"}, This kind of information,
Use JS code to Parse:
1 $.ajax ({2Url: "first.do",3Success:function (data) {//data refers to what is printed from the server to the browser4 //jsonstring Jsonobject5 //{"001": {"age": 122, "name": "smooth employment"}}6var result= eval ("(" +data+ ")");7 8 }9});
Here: Spring's Default JSON protocol parsing is done by Jackson.
Need to introduce three Jackson jar packs
Case Download: 05-mvc-annotation-handlerreturn-ajax
Http://pan.baidu.com/s/1cpCKTS
Spring MVC second day "annotation implementation SPRINGMVC handler handling Ajax simple request configuration"