RequestMapping annotation
Role: used to establish the correspondence between the request URL and the method of processing the request
Appearance:
Class: the first-level access directory of the request URL
Method: request the second level of the URL to access the directory
Attributes:
value: used to specify the requested URL, and the role of the path attribute is the same
method: used to specify the request method
params: Used to specify conditions for limiting request parameters. It supports simple expressions, requiring that the key and value of the request parameter must be exactly the same as the configuration
E.g:
headers: Used to specify the conditions that limit the request message headers
Binding of request parameters
The request parameters in the form are all based on key=value
The process of SpringMVC binding request parameters is to bind the request parameters by submitting the form to the method parameters in the controller.
E.g:
Supports bound data types
- Basic type parameters: including basic types and String types
- POJO type parameters: including entity classes and associated entity classes
- Array and collection type parameters: including a collection of List structure and Map structure (including arrays)
SpringMVC binding request parameters is automatically implemented, but if you want to use it, you must follow the usage requirements.
Requirements
If it is a basic type or String type: the name of our parameter must be consistent with the name of the formal parameter of the method in the controller. (Strictly case sensitive)
If it is a POJO type or its associated object: the parameter name in the form is required to be consistent with the attribute name of the POJO class, and the parameter type of the controller method is the POJO type
If it is a collection type, there are two ways:
Usage example
Basic type and String type as parameters
POJO type as parameter
Entity class code
jsp code
Controller code:
The POJO class contains collection type parameters
Entity class code
jsp code
Controller code
RequestParam annotation
Function: Give the parameter with the specified name in the request to the formal parameter in the controller
Attributes:
value: the name in the request parameter
require: Whether this parameter must be provided in the request parameter, default value: true, which means that it must be provided, if not provided, an error will be reported
Usage example
code in jsp
Code in the controller
operation result
RequestBody annotation
Function: used to obtain the content of the request body, and directly use the data with the structure of key=value&key=value......
Attributes:
required: Whether there must be a request body, the default value is: true, when the value is true, the get request method will report an error, if the value is false, the get request will get null
Usage example
jsp code
Controller code
Output
PathVaribale annotation
Function: Used to bind the placeholder in the url, for example, in the request url /delete/{id}, this {id} is the url placeholder
Attributes:
value: used to specify a placeholder in the url
required: whether a placeholder must be provided
Usage example
jsp code
Controller code
operation result
RequestHeader annotation
Role: used to get the message header
Attributes:
value: Provide the name of the message header
require: whether there must be this message header
Note: Generally not used in actual development
Usage example
code in jsp
Controller code
operation result
CookieValue annotation
Function: used to pass the value of the specified cookie name into the controller method parameter
Attributes:
value: specifies the name of the cookie
required: whether this cookie is required
Usage example
jsp code
Controller code
operation result
ModelAttribute annotation
Function: Appears on the method, which means that the current method will be executed before the controller method is executed. It can modify the method without return value, or modify the method with specific return value; appear on the parameter, get the specified data Assign values to parameters
Attributes:
value: The key used to obtain the data. The key can be the attribute name of the POJO or the key of the map structure.
SessionAttribute annotation
Role: used to execute parameter sharing between controller methods multiple times
Attributes:
value: used to specify the name of the stored attribute
type: the type of data used to execute the deposit
Usage example
code in jsp
Code in the controller
operation result