Spring MVC second day "annotation implementation SPRINGMVC handler handling Ajax simple request configuration"

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.