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