jquery Ajax and background interoperation in Spring MVC mode

Source: Internet
Author: User

1. Basic code

1) Background controller Basic code

@Controller @requestmapping ("/USER") Public classUsercontroller {@AutowiredPrivateUserService UserService;//Service@ResponseBody @RequestMapping (value= "/getuser/* access path set by itself * *", method =requestmethod.post) PublicObject getUser (@RequestBody user user) {User Resuser=NewUser (); Resuser= Userservice.getusers (User.getuserid ());//query users by userid     returnResuser; }}

2) User Class Code

 Public classUser {PrivateString UserName; PrivateString userId; PrivateString UserPassword;  PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }     PublicString getUserId () {returnuserId; }     Public voidSetuserid (String userId) { This. UserId =userId; }     PublicString Getuserpassword () {returnUserPassword; }     Public voidSetUserPassword (String userpassword) { This. UserPassword =UserPassword; }}

3) front-desk JS in jquery Ajax use code

  varparams ={userId:"12"//UserId should match the property name in the user Class 12 is the query value passed back, you can get the value through the input box to pass in            }; $.ajax ({type:"POST", Url:getrootpath ()+ "/user/getuser",//Access PathDataType: "JSON", ContentType:"Application/json", data:JSON.stringify (params), Timeout:commontimeout, Error:queryuserserror, Global:false, Success:queryuserssuccess,//Query Success processing function        });//Query Success processing functionfunctionqueryuserssuccess (Result) {//processing the returned data result       //display to page by processing result set returned by result}

2. Interactive process

The foreground will need to query the conditions through the Ajax back to the background, the background query after the results are assigned to the user object, the results for the return of the foreground is free to operate.

jquery Ajax and background interoperation in Spring MVC mode

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.