I. Overview
Note-Type controller support:
- Mapping and qualification of requests
- Automatic binding of parameters
- annotation Bindings for parameters
Ii. Mapping and qualification of requests
The HTTP request information contains six parts of information:
① Request method, ②url;③ protocol and version, ④ request header information (including cookie information), ⑤ return Line (CRLF); ⑥ request content area;
Among them,①,②, ④, ⑥ are generally variable, according to these information to map the processing method, specifically divided into:
- URL Path Mapping: Function processing method using URL mapping request to processor
- Request method Mapping Qualification: Processing a GET request only if the Qualified function processing method
- Request parameter mapping Qualification: Limit processing of requests that contain "ABC" Request parameters only
- Request Header Mapping Qualification: If only requests that handle "Accept=application/json" are qualified
1. URL Path Mapping
i) normal URL path mapping
@RequestMapping (value= "/user/create") @RequestMapping (value={"/test1", "/user/create"})
ii) URI template schema mapping, template variables can be extracted by @pathvariable
@RequestMapping (value= "/users/{userid}"): The requested URL can be "/users/123456" or "/USERS/ABCD" @RequestMapping (value= "/users/{ Userid}/create "): The requested URL can be"/users/123/create ". @RequestMapping (value= "/users/{userid}/topics/{topicid}"): The requested URL can be "/users/123/topics/123"
III) Ant-style URL path mapping (longest match first)
@RequestMapping (value= "/users/**"): Can Match "/USERS/ABC/ABC", but "/users/123" will be "/users/{userid}" in the URI template schema map Pattern precedence mapping to @requestmapping (value= "/product?") : Can match "/product1" or "/producta", but does not match "/product" or "/productaa" @RequestMapping (value= "/product*"): Can Match "/productabc" or "/ Product ", but does not match"/PRODUCTABC/ABC "@RequestMapping (value="/product/* "): Can match"/PRODUCT/ABC "but does not match"/PRODUCTABC "@ Requestmapping (value= "/products/**/{productid}"): Can Match "/products/abc/abc/123" or "/products/123", That is, ant style and URI template variable style can be mixed
IV) URL path mapping of regular expression style, can extract template variable by @pathvariable
@RequestMapping (value= "/products/{categorycode:\\d+}-{pagenumber:\\d+}"): Format {variable name: Regular expression}, can match "/products/123-1", But it doesn't match "/products/abc-1."
2. Request Method Mapping Qualification
@RequestMapping (value= "/hello", method = Requestmethod.post) @RequestMapping (value= "/hello", method = { Requestmethod.post, Requestmethod.get})
Dispatcherservlet support for GET, POST, PUT, DELETE, head is turned on by default
3. Request parameter Mapping Qualification
@RequestMapping (value = "/hello", params = "Create"): Indicates that the parameter name of "create" in the request can be matched, such as a matching request URL "http://xxx/parameter1?create "@RequestMapping (value ="/hello ", params ="!create "): Indicates that there is no" create "parameter name in the request to match @requestmapping (value ="/hello ", params = "Submitflag=create"): Indicates that there are "submitflag=create" request parameters in the request to match @requestmapping (value = "/hello", params = "submitflag!= Create "): Indicates that there is submitflag in the request but not equal to create to match @requestmapping (value ="/hello ", params = {" Test1 "," Test2=create "}): Indicates the "Test1" parameter name with "test2=create" argument to match
4. Request Header Mapping Qualification
@RequestMapping (value= "/hello", headers = "Accept"): Indicates that the requested URL must be "/hello" and that the request header must have an Accept parameter in order to match @requestmapping (value= "/hello", headers = "!ABC"): Indicates that the requested URL must be "/hello" and that the request header must have no ABC parameter to match @requestmapping (value= "/hello", headers = " Content-type=application/json "): Indicates that the requested URL must be"/hello "and must have" Content-type=application/json "in the request header parameter to match @requestmapping (value= "/hello", headers = "ACCEPT!=TEXT/VND.WAP.WML"): Indicates that the requested URL must be "/hello" and that the request header must have Accept but not equal to "TEXT/VND.WAP.WML" to match @requestmapping (value= "/hello", headers = {"Accept!=text/vnd.wap.wml", "abc=123"}) : The URL for the request must be "/hello" and the request header must have accept but not equal to "TEXT/VND.WAP.WML" and the request must have the parameter "abc=123" to match
Iii. Automatic binding of parameters
When a function method parameter list contains a specific type of parameter, the Spring WEB MVC framework automatically helps us pass the corresponding parameters, such as HttpServletRequest or httpservletresponse, to get input parameters or output data. But the use of inconvenient, recommended to use the annotation method
Introduction to Spring MVC usage (vi)--Annotated Controller (II)