SpringMVC basics- ing problem, springmvc basics ing
I. SpringMVC uses RequestMapping to solve the ing problem.
2. Before learning RequestMapping, Let's first look at a picture.
This figure shows the request URL, request Method, request header, and request body contained when an http request is sent. The figure is clearly marked.
Iii. RequestMapping
1. The translation is request ing. In my opinion, the ing is the http request and the handler method for processing the request.
2. @ RequestMapping can be marked at the Handler class definition that processes the request, or at the handler method definition.
(1) annotation in Handler class definition: provides basic request ing information, relative to the root directory of the web application. In real projects, it is equivalent to namespace and is generally used to divide modules. For example:
Request URL: http: // localhost: 8080/springmvc/test/method
Target Handler:
/** * @author solverpeng * @create 2016-08-03-11:06 */@Controller@RequestMapping("/test")public class RequestMappingTest2 { @RequestMapping("/method") public String testRequestMapping() { System.out.println("test requestMapping.."); return "success"; }}
Note that the @ RequestMapping value attribute in the handler method is relative to the path in the class definition.
(2) annotation in the handler method definition: Provide the request ing information, relative to the root directory of the web application. For example:
Request URL: http: // localhost: 8080/springmvc/hello
Target Handler:
@RequestMapping("/hello")public String hello() { System.out.println("hello springmvc!!!"); return "success";}
3. @ RequestMapping attribute details.
Before explaining each attribute, let's take a look at the annotation class @ ReqeustMapping.
SpringMVC can map all http request information in the Servlet environment and map multiple parameters under the same rule using arrays.
(1) value Attribute
The official documentation explains this as follows:
The primary mapping expressed by this annotation.
<P> In a Servlet environment: the path mapping URIs (e.g. "/myPath. do ").
Ant-style path patterns are also supported (e.g. "/myPath/*. do ").
At the method level, relative paths (e.g. "edit. do") are supported
Within the primary mapping expressed at the type level.
Path mapping URIs may contain placeholders (e.g. "/$ {connect }")
The explanation is quite clear. In the Servlet environment, URI ing is supported, Ant style ing is supported, and relative path ing is also supported at the method level. The ing information can even be obtained from external files.
Ant style is described here:
Matching in Ant style resource address 3 is supported:
? : Match a character
*: Match any character
**: Match multiple layers of paths
In the value attribute of @ RequestMapping:
/Test/*/add: Match URLs such as/test/test01/add and/test/test02/add
/Test/**/add: Match URLs such as/test/springmvc/test01/add and/test/springmvc/test02/add
/Test/springmvc/add ?? : Match URLs such as/test/springmvc/addaa and/test/springmvc/addbb
Specific Use: You can map a single uri or multiple Uris.
@RequestMapping("/hello")public String hello() { System.out.println("hello springmvc!!!"); return "success";}
@RequestMapping(value = {"/testUrl01", "/testUrl02"})public String testRequestMappingUrl() { System.out.println("test multi url..."); return "success";}
(2) method attributes
The official documentation explains this as follows:
The HTTP request methods to map to, narrowing the primary mapping:
GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
<P> <B> Supported at the type level as well as at the method level! </B>
When used at the type level, all method-level mappings inherit
This HTTP method restriction (I. e. the type-level restriction
Gets checked before the handler method is even resolved ).
<P> Supported for Servlet environments as well as Portlet 2.0 environments.
To PUT it simply, the method attribute is a ing of the http request method. It mainly maps GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, and TRACE methods.
These methods are stored in the RequestMethod enumeration class as enumeration:
public enum RequestMethod { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE}
It can be used at the class level or at the method level. When used at the class level, all method-level mappings must inherit from it.
Specific Use: You can map one method or multiple methods.
@RequestMapping(value = "/testMultiMethod", method = {RequestMethod.GET, RequestMethod.POST})public String testRequestMappingMultiMethod() { System.out.println("test multi method..."); return "success";}@RequestMapping(value = "/testUrl", method = RequestMethod.POST)public String testRequestMappingUrlAndMethod(){ System.out.println("test url and method..."); return "success";}
(3) params attributes
There are too many explanations in the official documentation. Here we only describe:
The params property maps Request Parameters. See the following mappings:
"MyParam = myValue" style expressions: The request parameter must contain myParam and the value must be myValue.
"MyParam! = MyValue "style expressions: The request parameter must contain myParam and the value must not be myValue.
"! MyParam "style expressions: The request parameter must not contain myParam.
It can be used at the class level or at the method level. The method must inherit from the class level.
Take the following example:
@ RequestMapping (value = "/testMultiParam", params = {"userName", "age! = 23 "}) public String testRequestMappingParam5 () {System. out. println ("test multi params... "); return" success ";}/*** the request parameter must contain userName but cannot be equal to jack. */@ RequestMapping (value = "/testParam4", params = "userName! = Jack ") public String testRequestMappingParam4 () {System. out. println ("test param4... "); return" success ";}/*** the request parameter cannot contain userName. */@ RequestMapping (value = "/testParam3", params = "! UserName ") public String testRequestMappingParam3 () {System. out. println ("test param3... "); return" success ";}/*** the request parameter must be userName and the value is jack. Otherwise, the System Returns Error 404 */@ RequestMapping (value = "/testParam2", params = "userName = jack") public String testRequstMappingParam2 () {System. out. println ("test param2... "); return" success ";}/*** if the request parameter does not contain userName, the system will return the 404 error */@ RequestMapping (value ="/testParam ", params = "userName") public String testRequestMappingParam () {System. out. println ("test param... "); return" success ";}
(4) header attributes
The header attribute is used to map http request headers. It is divided into the following mappings:
"My-Header = myValue": the property value of the ing Request Header must be myValue. Otherwise, 404 is returned.
"My-Header! = MyValue ": the value of the ing Request Header property My-Header must not be myValue; otherwise, 404 is returned.
"! My-Header ": The ing request must not have the My-Header attribute; otherwise, 404 is returned.
For example:
@RequestMapping(value = "/testHeader", headers = "Accept-Language=zh,zh-CN;q=0.8")public String testRequestMappingHeader() { System.out.println("test header ..."); return "success";}
@RequestMapping(value = "/testHeader", headers = "Accept-Language!=zh,zh-CN;q=0.8")public String testRequestMappingHeader() { System.out.println("test header ..."); return "success";}
@RequestMapping(value = "/testHeader", headers = "!Accept-Language")public String testRequestMappingHeader() { System.out.println("test header ..."); return "success";}
Media type wildcard is also supported, such:
RequestMapping(value = "/something", headers = "content-type=text/*")
Will match: "text/html", "text/plain", and so on.
(5) consumes attributes
The consumes attribute is used to map consumer media types, which refer to requests, such:
consumes = "text/plain"consumes = {"text/plain", "application/*"}
You can also use "! "Operator, such as in "! In text/plain, used to filter Content-Type other than "text/plain.
Content-Type: The Content Type of the request Header, indicating the media Type of the Content data sent to the server. on the server side, you can use request. getContentType () to read the Content.
Use consumes = "text/plain" to process only the data whose Content-Type is text/plain. That is (Consume request content data)
(6) produces attributes
The produces attribute is used to map the producer media type, which refers to the response, for example:
produces = "text/plain"produces = {"text/plain", "application/*"}
You can also use "! "Operator, such as in "! In text/plain, used to filter the types of Accept except "text/plain.
Produce the response header Content-Type on the server and specify the returned data (the server is the producer). The client is the consumer.
In general:
① Client-Send request-server: the client uses Content-Type in the request header to specify the media Type of the Content body (that is, the client is the producer at this time ), the server consumes the Content body data based on the Content-Type (that is, the server is a consumer at this time ).
② Server-Send request-client: the server produces the response body data specified by Content-Type in the Response Header (that is, the server is a producer at this time ), the client consumes the Content body data based on the Content-Type (that is, the client is a consumer at this time ).
Problem:
① On the server side, you can specify [headers = "Content-Type = application/json"] to declare a media Type that can be processed (consumable, that is, only the Request body data specified by Content-Type is consumed.
② How does the client tell the server that it only consumes media data? That is, what type of data does the client accept (required? What type of data should the server produce? In this case, we can request the Accept request header to implement this function.
For details about these two attributes, refer:
Http://jinnianshilongnian.iteye.com/blog/1695047
Iv. Summary:
This section does not explain the springmvc config file and the web. xml configuration. It is the basis of the previous article.
SpringMVC uses RequestMapping to resolve the ing problem between http requests and target processing methods. Through @ RequestMapping, each attribute contains all the request content, and even assigns an independent attribute to a specific type, such as ing consumer and producer media types.
This method is elegant and flexible. Each attribute is in the String [] format and supports matching multiple values at the same time.Or". If different attributes exist at the same time,And. Before learning this section, we recommend that you have a complete understanding of http requests.
On the homepage of the article, an http request is described using a graph.
For more information, see:
Http://www.cnblogs.com/solverpeng/p/5613568.html
For more information about url-pattern, see:
Http://www.cnblogs.com/solverpeng/p/5729763.html