Spring Basic Series--several methods of obtaining spring MVC request parameters

Source: Internet
Author: User

Several methods of obtaining Spring MVC request parameters reprint: Http://www.cnblogs.com/leiOOlei/p/3658147.html One, get the parameters in the path by @pathvariabl
    @RequestMapping (value= "User/{id}/{name}", method=requestmethod.get) public    String PrintMessage1 (@PathVariable String ID, @PathVariable string name, Modelmap model) {                System.out.println (ID);        SYSTEM.OUT.PRINTLN (name);        Model.addattribute ("message", "111111");        Return "users";    }

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

Second, @ModelAttribute get the form form data of the POST request

The JSP form is as follows

<form method= "POST" action= "hao.do" >    A: <input id= "A" type= "text"   name= "a"/>    B: <input id= "B" type= "text"   name= "B"/>    <input type= "Submit" value= "Submit"/> </form>

Java Pojo is as follows

    public class pojo{        private String A;        private int b;    }

The Java controller is as follows

@RequestMapping (method = requestmethod.post) public String processsubmit (@ModelAttribute ("Pojo") Pojo Pojo) {         return "HelloWorld"; }
Third, direct use of httpservletrequest access
@RequestMapping (method = requestmethod.get) public String GET (httpservletrequest request, httpservletresponse response ) {    System.out.println (Request.getparameter ("a"));     return "HelloWorld"; }

Iv. binding request parameters with annotations @requestparam

Bind request parameter A to variable a with annotation @requestparam

An exception occurs when the request parameter A does not exist, which can be resolved by setting the property Required=false.

Example: @RequestParam (value= "a", Required=false)

Controller as follows

@RequestMapping (value = "/requestparam", method = requestmethod.get) public string Setupform (@RequestParam ("a") string a , Modelmap model) {    System.out.println (a); return "HelloWorld";}

Spring Basic Series--several methods of obtaining spring MVC 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.