Springmvc several ways to receive request parameters

Source: Internet
Author: User

1. Binding request parameters with annotations @requestparam

Bind request parameter A to variable a with annotation @requestparam, when request parameter A does not exist an exception occurs, can be resolved by setting properties Required=false, for example: @RequestParam (value= "a", required= False

JS and Controller are as follows: JS when the post data is a JSON object:

correspond to the wording in the controller:

2. @ModelAttribute get form form data for a POST request

The JSP page is as follows:

Where the OnSubmit property is used to execute a piece of JavaScript when the form is submitted.

The corresponding controller is as follows: The name attribute value in the form is the attribute in the user of the controller in the Pojo, so that the background automatically receives the data from the form and encapsulates it into user

3. Get the parameters in the path by @pathvariabl

For example:

1@RequestMapping (value= "User/{id}/{name}", method=requestmethod.get)2 3      Publicstring PrintMessage1 (@PathVariable string ID,@PathVariable string name, Modelmap model) {4 5 6 7 System.out.println (ID);8 9 System.out.println (name);Ten  OneModel.addattribute ("message", "111111"); A  -         return"Users"; -  the}

When accessing the User/123/lei path, execute the above method, where the parameter Id=123,name=lei

4. The most primitive method: Get it directly with HttpServletRequest

1 @RequestMapping (method =2public3    System.out.println ( Request.getparameter ("A"4     return "HelloWorld"5 }

Reference: https://www.cnblogs.com/blog411032/p/5909512.html

Springmvc several ways to receive request parameters

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.