Spring receives AJAX parameters in several ways

Source: Internet
Author: User

Reference URL: Spring receives AJAX parameters in several ways@ModelAttribute Annotations

Using @modelattribute This method can directly map parameters into Pojo objects, I do not add @modelattribute annotations, directly receive Pojo objects, can also receive parameters

front-End Ajax requests
        <Scripttype= "Text/javascript">            $(function() {$.ajax ({type:"Post", URL:"http://localhost:8080/test", data:{"name":"Zhang San",                        "Phone":"10086",                        "Password":"123456"}, Async:true, Success:function(data) {Console.log (data);            }                }); })        </Script>
Java Receive parameters
    @RequestMapping ("/test")    @ResponseBody    public String testUser (@ModelAttribute ("TUser") ) {TUser user) {        System.out.println (User.getname ());        System.out.println (User.getpassword ());        System.out.println (User.getphone ());         return "OK";    }
Mapping results

@PathVariabl Annotations

@PathVariable is to map the specified segment point on the requested path to the specified parameter name, @PathVariable can specify multiple, and if a parameter is not placed in the URL path, request access directly. http://localhost:8080/test. can also receive the parameters

front-End Ajax requests
        <Scripttype= "Text/javascript">            $(function() {$.ajax ({type:"Post", URL:"http://localhost:8080/test/10086", data:{"name":"Zhang San",                        "Password":"123456"}, Async:true, Success:function(data) {Console.log (data);            }                }); })        </Script>
Java Receive parameters
    @RequestMapping ("/test/{phone}")    @ResponseBody    public  string TestUser (String name,@ pathvariable String phone,string password) {        System.out.println (name);        SYSTEM.OUT.PRINTLN (phone);        SYSTEM.OUT.PRINTLN (password);         return "OK";    }
Mapping results

get it directly with HttpServletRequestFront-end code equivalent to case one Java receive parameter
    @RequestMapping ("/test")    @ResponseBody    public  String testUser (httpservletrequest Request, HttpServletResponse response) {        System.out.println (request.getparameter ("name"));        System.out.println (Request.getparameter ("Phone"));        System.out.println (Request.getparameter ("password"));         return "OK";    }
The mapping results are exactly the same as case one@RequestParam Binding Request ParametersThe front-end request code is equivalent to case one Java receive parameter
    @RequestMapping ("/test")    @ResponseBody    public String testUser (@RequestParam ("name") String A, @RequestParam ("Phone") string B, string password) {        /**         * @RequestParam () The value inside must be the same         as the parameter passed by the front end . *        /System.out.println (a);        System.out.println (b);        SYSTEM.OUT.PRINTLN (password);         return "OK";    }
The mapping results are exactly the sameSummary:

1. If the parameters passed by the front end are the same as the parameter names defined by the background interface , no annotations are required, and if all the parameters are in a Pojo object, they can be mapped directly to the Pojo object , or you can use the HttpServletRequest Receive parameters

2. If you want to use the Resfull style request method, you can use the @PathVariable to annotate

3. If the parameters passed by the front end are not the same as the parameter names defined by the background interface, then the Requestparam annotations are used

Spring receives AJAX parameters in several ways

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.