When using the @requestmapping URI template style mapping, which is Someurl/{paramid}, the Paramid can bind the value passed to the method by @Pathvariable annotations.
Example code:
@Controller @RequestMapping ( "/owners/{ownerid}" ) public Class Relativepathuritemplatecontroller {@RequestMapping ( "/pets/{ Petid} ") public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) {
// implementation omitted }}
The above code binds the value of the variable ownerid in the URI template and the value of Petid to the parameter of the method. If the variable names in the method parameter names and URI template that need to be bound are inconsistent, you need to specify the name in the URI template in @pathvariable ("name").
2, @RequestHeader, @CookieValue
@RequestHeader annotations, you can bind the value of the header portion of request requests to the parameters of the method.
Sample code:
This is the header part of a request:
Host localhost:8080
Accept Tex t/html,application/xhtml+xml,application/xml;q=0.9
Accept-language fr,en-gb;q=0.7,en;q=0.3
Accept-Encodi ng gzip,deflate
Accept-charset iso-8859-1,utf-8;q=0.7,*;q=0.7
keep-alive
@RequestMapping ("/displayheaderinfo.do") publicvoid Displayheaderinfo (@ Requestheader ("accept-encoding") String Encoding, @RequestHeader (long keepAlive) { }
The above code binds the accept-encoding value of the request header part to the parameter encoding, and the value of the keep-alive header is bound to the parameter keepalive.
@CookieValue can bind the value of the cookie in the request header to the parameters of the method.
For example, there are the following cookie values:
Code for parameter binding:
@RequestMapping ("/displayheaderinfo.do") publicvoid Displayheaderinfo (@ Cookievalue ("Jsessionid") String cookie) { }
That is, bind the value of the Jsessionid to the parameter cookie.
3, @RequestParam, @RequestBody
A) commonly used to deal with simple types of bindings, a String obtained by Request.getparameter () can be converted directly into a simple type case (string--> The conversion of a simple type is done by a converter configured by Conversionservice), because the parameter is obtained using the Request.getparameter () method, so you can handle the value of querystring in the Get mode, or you can handle the post mode The value of body data;
B) used to deal with Content-type: for application/x-www-form-urlencoded encoded content, submit the way get, POST;
C) The note has two properties: value, required, value specifies the ID name to pass in the value, and required is used to indicate whether the parameter must be bound;
Example code:
@Controller @RequestMapping ( "/pets" "pet" public Span style= "COLOR: #0000ff" >class Editpetform {@RequestMapping (method =< Span style= "COLOR: #000000" > Requestmethod.get) public String Setupform (@ Requestparam ("Petid") int Petid, Modelmap model) { Pet Pet = this .clinic.loadpet (Petid); Model.addattribute ( "pet" return "Petform" ; }
@RequestBody
This annotation is commonly used to deal with Content-type: not application/x-www-form-urlencoded encoded content, such as Application/json, application/xml, etc.;
It parses the post data body by using the httpmessageconverters of the Handleradapter configuration, and then binds to the corresponding bean.
Because the configuration has formhttpmessageconverter, so it can also be used to handle the contents of application/x-www-form-urlencoded, processed results are placed in a multivaluemap<string, String>, this situation is used under certain special needs, see Formhttpmessageconverter API for details;
SPRINGMVC Usage of common annotations