"Springmvc" SPRINGMVC series 2 @requestmapping mapping constraint request

Source: Internet
Author: User

    1. @RequestMapping Mapping Constraint requests

2.1. Map Request URL

Spring MVC uses @RequestMapping annotations to specify which URL requests can be processed by the controller, which can be labeled at the controller's class definition and method definition.

    • At the class definition: Provides preliminary request mapping information. The root directory relative to the WEB app
    • Method: Provides further subdivision mapping information. Relative to the URL at the class definition. If @RequestMapping is not marked at the class definition, the URL marked at the method is relative to the Web app's root directory

Dispatcherservlet intercepts the request, it determines the processing method corresponding to the request through the mapping information provided by @RequestMapping on the controller. A simple example is as follows:

2.2. Map request method, request header, request parameters

@RequestMapping can use request methods, request parameters, and request header mapping requests in addition to requesting URL mapping requests.

The value, method, params, and heads of the @RequestMapping respectively represent the request URL, request methods, request parameters, and the mapping condition of the request header, which is the relationship between them, and the combination of multiple conditions can make the request mapping more precise.

The params and headers support simple expressions:

    • PARAM1: Indicates that the request must contain a request parameter named param1
    • !PARAM1: Indicates that the request cannot contain a request parameter named param1
    • Param1! = value1: Indicates that the request contains a request parameter named param1, but its value cannot be value1
    • {"Param1=value1", "param2"}: The request must contain two request parameters named Param1 and param2, and the value of the param1 parameter must be value1

2.3. Support ant-style URL matching

ANT-style resource addresses support 3 matching characters:

    • ?: matches one character in a file name
    • *: matches any character in the file name
    • **:** matching multi-layer paths

@RequestMapping also supports ANT-style URLs, examples:

/user/*/createuser: Match

URLs such as/user/aaa/createuser,/user/bbb/createuser, etc.

/user/**/createuser: Match

URLs such as/user/createuser,/user/aaa/bbb/createuser, etc.

/user/createuser??: Match

URLs such as/user/createuseraa,/USER/CREATEUSERBB, etc.

The essence of @RequestMapping is to constrain HTTP request URLs, headers, parameters, methods, and so on.

"Springmvc" SPRINGMVC series 2 @requestmapping mapping constraint request

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.