RequestMapping annotation in SpringMVC [function, location, attribute]

RequestMapping annotation

effect

Used to establish the correspondence between the request URL and the method of processing the request.

Appearance position

1. Act on the class: request the first level of the URL to access the directory. If not written here, it is equivalent to the root directory of the application. You need to start with / when you write.
2. Act on the method: request the second level of the URL to access the directory.

Some code examples in the controller:

@Controller
@RequestMapping(path = "/user") //第一级的访问目录
public class HelloController {
    
    

    @RequestMapping(path = "/hello") //第二级的访问目录
    public String sayHello(){
    
    
        System.out.println("Hello SpringMVC");
        return "success";
    }

    @RequestMapping(value = "/testRequestmapping") //第二级的访问目录
    public String testRequestmapping(){
    
    
        System.out.println("测试注解");
        return "success";
    }
}

Some code examples in jsp:

<!-- 第一种访问方式 --> 
<a href="${pageContext.request.contextPath}/user/hello">sayHello</a> <br/>
<!-- 第二种访问方式 --> 
<a href="user/testRequestmapping">测试注解</a>

Details to note:

1. Details: The path can not be written/ means the root directory of the application starts
. 2. Details: ${ pageContext.request.contextPath} can also be omitted, but the path cannot be written/

Attributes

value:Used to specify the requested URL. It has the same function as the path attribute.
method: Used to specify the request method.
params:Used to specify conditions for limiting request parameters. It supports simple expressions. The key and value of the request parameter must be exactly the same as the configuration.

For example:
params = {"username"}, which means that the request parameter must have username.
params = {"age!100"}, which means that the age in the request parameter cannot be 100

headers: Used to specify the conditions for restricting the request message header.

Note: As long as there are two or more of the above four attributes, their relationship is with.

Examples of value or path attributes

The example of the position above is an example of the value or path attribute.

Example of method attribute

Part of the code of the controller:

@RequestMapping(value="/saveAccount",method=RequestMethod.POST)
public String saveAccount() {
    
    
	System.out.println("保存了账户");
	return "success"; 
}

Part of the jsp code:

<!-- 请求方式的示例 --> 
<a href="account/saveAccount">保存账户,get 请求</a> <br/>
<form action="account/saveAccount" method="post"> 
	<input type="submit" value="保存账户,post 请求">
</form>

Note: When using the get request method="get", the error message is 405, and the message is that the method does not support the get request.
Insert picture description here

Example of params attribute

Part of the code of the controller:

@RequestMapping(value="/removeAccount",params= {
    
    "accountName","money>100"})
public String removeAccount() {
    
    
	System.out.println("删除了账户");
	return "success"; 
}

Part of the jsp code:

<!-- 请求参数的示例 --> 
<a href="account/removeAccount?accountName=aaa&money>100">删除账户,金额 100</a> <br/>
<a href="account/removeAccount?accountName=aaa&money>150">删除账户,金额 150</a>

Note:
When we click the first hyperlink, we can visit successfully.
When we clicked on the second hyperlink, it was not accessible. As shown below:

Insert picture description here

The situation of params key-value pairs and headers attributes

There are also params = {"username=keafmd"}key-value pairs: this example means that not only the username attribute but also the value of Keafmd is required.
At the same time, the headers attribute means that the corresponding request header must be included.
Part of the code of the controller:

@Controller
@RequestMapping(path = "/user")
public class HelloController {
    
    

    @RequestMapping(value = "/testRequestmapping",method = {
    
    RequestMethod.GET},params = {
    
    "username=keafmd"},headers = {
    
    "Accept"})
    public String testRequestmapping(){
    
    
        System.out.println("测试注解");
        return "success";
    }
}

Part of the jsp code:

<a href="user/testRequestmapping?username=keafmd">RequestMapping注解</a>

This can be successfully accessed.

The above is all the contents of the RequestMapping annotation [function, location, attribute] in SpringMVC.

After reading, if it is helpful to you, thank you for your support!
If you are on a computer side, you will see "One key triple connection"Is it, right click it [haha]

Insert picture description here

Come on!

Work together!

Keafmd

Guess you like

Origin blog.csdn.net/weixin_43883917/article/details/113096620