Spring MVC @Controller and @RequestMapping annotations

@Controller annotation

The @Controller annotation can identify an ordinary Java class as a controller (Controller) class. The sample code is as follows.

package net.biancheng.controller;
import org.springframework.stereotype.Controller;
@Controller
public class IndexController {
    // 处理请求的方法
}

Spring MVC uses the component scanning mechanism to find the controller class in the application. In order to ensure that the controller can be scanned by Spring MVC, we also need to use tags in the Spring MVC configuration file to specify the basic package of the controller class (please  <context:component-scan/> ensure All controller classes are under the basic package and its sub-packages), the sample code is as follows

<!-- 使用扫描机制扫描控制器类,控制器类都在net.biancheng.controller包及其子包下 -->
<context:component-scan base-package="net.biancheng.controller" />

@RequestMapping annotation

The @RequestMapping annotation is one of the most commonly used annotations in Spring MVC. It is usually marked on the controller method and is responsible for associating the request with the controller method that handles the request and establishing a mapping relationship.

After the Spring MVC front-end controller (DispatcherServlet) intercepts the request from the user, it will find the corresponding controller method through the mapping information provided by the @RequestMapping annotation and process the request.

How to use @RequestMapping annotation

@RequestMapping can be annotated on the controller class or the controller method.

1. Modification method

When the @RequestMapping annotation is annotated on a method, the value attribute value represents the URL address to access the method. When the user sends a request to access the controller method under the Controller, the request path must be the same as this value. The sample code is as follows

@Controller
public class HelloController {
    @RequestMapping("/login")
    public String welcome() {
        return "login";
    }
}

2. Modification class

When the @RequestMapping annotation is annotated on a controller class, the value of the value attribute is the parent path of the URL addresses of all controller methods in this controller class. In other words, accessing any controller method under this Controller requires the parent path.

@Controller
@RequestMapping(value = "/springmvc")
public class HelloController {
    @RequestMapping("/login")
    public String welcome() {
        return "login";
    }
}

In the above control class, if the user wants to access the welcome() method in HelloController, the requested address must bring the parent path "/springmvc", that is, the requested address must be "/springmvc/login".

@RequestMapping annotated properties

The @RequestMapping annotation provides multiple available attributes. Below we will introduce some of the more commonly used attributes.

1. value attribute

In the @RequestMapping annotation, the value attribute is used to set the request mapping address of the controller method. All requests that match the request mapping address can be processed by this controller method. The sample code is as follows.

@RequestMapping(value = "/register")

The value attribute is the default attribute of the @RequestMapping annotation. If we set only one value attribute in the @RequestMapping annotation, the attribute name can be omitted. The value of the value attribute is an array of string type, indicating that the controller method can match multiple request addresses.

@RequestMapping( value = {"/register", "/login"})
public String success() {
    return "success";
}

2. name attribute

The name attribute is equivalent to the annotation of the method, which is used to explain what this method is used to make the method easier to understand.

For example, the following code indicates that the getUsers() method is a controller method used to obtain user information.

@RequestMapping(value = "toUser",name = "获取用户信息")
public String getUsers() {
    ……
}

3. method attribute

The method attribute is used to set the request methods supported by the controller method. If a controller method does not set the method attribute of the @RequestMapping annotation, it means that the controller method supports all request types and can handle all types of requests.
The value of the method attribute is an array of RequestMethod type, indicating that a controller method supports multiple requests. Common request methods include GET, POST, DELETE, PUT, etc.

For example, the controller method only supports GET request, the code is as follows.

@RequestMapping(value = "/toUser",method = RequestMethod.GET)

 We can also specify support for multiple types of requests for the same controller method. For example, a method supports both GET and POST requests. The code is as follows.

@RequestMapping(value = "/toUser",method = {RequestMethod.GET,RequestMethod.POST})

4. params attribute

The params attribute is used to specify the parameters in the request. Only when the request carries qualified parameters, the controller method will process the request.

serial number expression meaning
 "param" The request must carry a parameter named param
"!param" Completely opposite to the meaning of expression ①, the parameter named param cannot be carried in the request
"param=value" The request must carry a parameter named param, and the value of the parameter must be: value
"param!=value" Completely opposite to the meaning of expression ③, parameters cannot be carried in the request: param = value.

The value of the params attribute is an array of string type, indicating that the controller method will process the request only if the request carries all parameters specified by the params attribute.
For example

@RequestMapping(value = "/testParam", params = {"name=zhang", "age=23"})
@ResponseBody
public String testParam() {
    return "success";
}

The above code indicates that the controller method testParam() will process the request only when the request carries both the name and age request parameters, and the parameter values ​​must be "zhang" and "23" respectively.

5. headers attribute

The headers attribute is used to set the request header information in the request. Only when the request carries the specified request header information, the controller method will process the request.

serial number expression meaning
 "header" The request must carry request header information: header 
"!header" Completely opposite to the meaning of expression ①, the request cannot carry request header information: header
"header=value" The request must carry request header information: header=value.
"header!=value" Completely opposite to the meaning of expression ③, the request cannot carry request header information: header=value.

The header attribute is an array of character types, indicating that the controller method will process the request only if the request also carries all the header information specified in the array. The request will be processed only if the Referer header information of the request contains relevant content.

Guess you like

Origin blog.csdn.net/qq_43079001/article/details/132684606