In the previous Hello World example, the @RequestMapping annotation was used to establish a mapping between the request and the controller method that handles the request.

When SpringMVC receives a specified request, it finds the corresponding controller method in the mapping to handle the request.

RequestMapping annotation location

In the example, annotations are used on methods, but they can also be used on classes.

1. Function in method

@Controller public class RequestMappingController { @RequestMapping("/testRequestMapping") public String testRequestMapping(){ return "success"; }}Copy the code

The request path of the request mapping is /testRequestMapping.

2. Act on classes

@controller @requestMapping ("/test") public class RequestMappingController {// /test/testRequestMapping @RequestMapping("/testRequestMapping") public String testRequestMapping(){ return "success"; }}Copy the code

The request path of the request mapping is /test/testRequestMapping.

If you have two modules: users and orders, each module will have its own list interface /list.

To better distinguish modules by name, annotate the two classes so that their final paths are /user/list and /order/list.

Of course, you can also use method annotations without class annotations, such as /userList and /orderList.

In general, a request can only be handled by one controller, and if you have two different controller methods using /list, then the request will not know which one to handle and will report an error.

RequestMapping specifies the value attribute of the @requestMapping annotation

The value attribute matches the request mapping by the request address. This attribute must be set; otherwise, the request address cannot match the mapping.

In addition, the value property is also an array of strings, indicating that the request map can match requests at multiple request addresses.

@Controller public class RequestMappingController { @RequestMapping( value = {"/test1", "/test2"} ) public String testRequestMapping(){ return "success"; }}Copy the code

TestRequestMapping () returns success.html regardless of whether /test1 or /test2 is requested.

RequestMapping method attribute

The Method attribute matches the request map by how the request is requested, such as GET or POST.

The Method attribute is an array of type RequestMethod, indicating that the request map can match requests of multiple request styles.

If the request address meets the value attribute of the request mapping, but the request method does not meet the method attribute, for example, the controller is set to GET request method, but the request is sent to POST:

@Controller public class RequestMappingController { @RequestMapping( value = {"/test1"}, method = {RequestMethod.GET} ) public String testRequestMapping(){ return "success"; }}Copy the code

Index.html add post to send:

<! DOCTYPE HTML > < HTML lang="en" XMLNS :th="http://www.thymeleaf.org"> <head> <meta charset=" utF-8 "> <title> </head> <body> <h1>Hello World</h1> <a th:href="@{/target}"> Access target. HTML </a> <br> <a Th: href = "@ {/ test1}" > test the value attribute of the @ RequestMapping -- > / test1 < / a > < br > < a Th: href = "@ {/ test2}" > test the value attribute of the @ RequestMapping -- > / test2 < / a > < br > < a Th :href="@{/test}"> test @requestMapping method attribute -->/test</a><br type="submit"> </form> </body> </html>Copy the code

405: Request method ‘POST’ not supported

If you continue to add support for the POST method, you can access:

@Controller public class RequestMappingController { @RequestMapping( value = {"/test1"}, method = {RequestMethod.GET, RequestMethod.POST} ) public String testRequestMapping(){ return "success"; }}Copy the code

RequestMapping is a derived annotation in combination with the request method

For controller methods that handle specified requests, SpringMVC provides a derived annotation of @RequestMapping.

  • @getMapping: Processes the mapping of GET requests
  • PostMapping: Processes the mapping of POST requests
  • @putMapping: Processes the mapping of put requests
  • @deletemapping: Mapping to handle delete requests

With this annotation, you don’t need to set the Method property.

    @GetMapping("/test3")
    public String testGetMapping() {
        return "success";
    }
Copy the code

RequestMapping params attribute

The most commonly used attributes are value and method.

The params property matches the request map by the request parameters of the request. It is a string array that sets the matching relationship between the request parameters and the request map using four expressions:

  • Param: Requests matched by the request map must carry parAM request parameters
  • ! Param: Requests that the request map matches must not carry parAM request parameters
  • Param =value: Requests matched by the request map must carry parAM request parameters and param=value
  • param! =value: requests that are matched by the request map must carry param request parameters but param! =value

Here’s an example:

@RequestMapping( value = {"/test1"}, method = {RequestMethod.GET, RequestMethod.POST}, params = {"username", "password! =123456"} ) public String testRequestMapping(){ return "success"; }Copy the code

Params = {“username”, “password! =123456”} the request must have parameters username and password, and the value of password must be different from 123456.

RequestMapping headers attribute

Let’s also understand.

The headers attribute matches the request mapping with the request header information of the request. It is a string array that sets the matching relationship between the request header information and the request mapping using four expressions:

  • Header: Requests matched by the request mapping must contain header request information
  • ! Header: Requests matched by the request mapping must not contain header request information
  • Header =value: Requests matched by the request mapping must carry header information and header=value
  • header! =value: Requires that the request map matches a request that contains a header request header and a header! =value

If the request meets the value and method attributes of @RequestMapping but does not meet the header attributes, a 404 error is displayed indicating that the resource is not found.

@RequestMapping( value = {"/test1"}, method = {RequestMethod.GET, RequestMethod.POST}, params = {"username", "password! }, headers = {"Host=localhost:8081"}) public String testRequestMapping(){return "success"; }Copy the code

My local port is 8080, now to request the page, will report an error.


Thanks to Silicon Valley for learning resources.