Spring MVC -- Request Mapping

Keywords: Java Attribute Spring

In Spring MVC, how to handle requests is an important task. Request mapping is annotated with @RequestMapping. Among them, the annotation on the class is equivalent to a prefix, indicating that the processor is handling the same kind of requests; the annotation on the method is more detailed. For example, the label of a class may be "user", indicating that all operations are user-related; the specific method may be "create", "update", "delete", etc., indicating which kind of operation is performed on the user.

package cn.javass.chapter6.web.controller;  
@Controller  
@RequestMapping(value="/user")                 //①Universal Mapping Prefix for Processors  
public class HelloWorldController2 {  
    @RequestMapping(value = "/hello2")        //②Be relative to①Narrowing mappings at  
    public ModelAndView helloWorld() {  
         //Ellipsis realization  
    }  
}  

Now let's summarize what the request mapping is.

I. URL Path Mapping

This mapping involves only value.

@RequestMapping(value={"/test1", "/user/create"}) //OrRepresents that multiple paths can be mapped to the same processing method
@RequestMapping(value="/users/{userId}/topics/{topicId}") //You can also use braces to represent variable placeholders
@RequestMapping(value="/product?") //Matching“/product1"Or "/producta",But they don't match.“/product"Or "/productaa"
@RequestMapping(value="/products/**/{productId}") //Matching“/products/abc/abc/123"Or "/products/123"
@RequestMapping(value="/products/{categoryCode:\\d+}-{pageNumber:\\d+}") //regular expression

2. Request Method Mapping Limitation

Provide not only the value attribute, but also the method attribute.

@RequestMapping(value="/create", method = RequestMethod.GET) //Represents processable matching“/create"And the request method is“ GET"Request
@RequestMapping(value="/create", method = RequestMethod.POST) //Represents processable matching“/create"And the request method is“ POST"Request

Generally, browsers only support GET and POST types, while others such as PUT and DELETE need to be simulated.

3. Request parameter mapping restriction

You need to provide params and method attributes.

Take the following controller as an example.

@Controller  
@RequestMapping("/parameter1")       //①Universal Mapping Prefix for Processors  
public class RequestParameterController1 {
    // Something...
}
@ RequestMapping(params="create", method=RequestMethod.GET)// indicates that the request has a parameter name of "create" and the request method is "GET", which matches.
                                                             For example, the matchable request URL "http://*/parameter1?create"

@ Request Mapping (params = "create", method = RequestMethod. POST)// indicates that the request has a parameter name of "create" and the request method is "POST" to match.

@ RequestMapping(params="!create", method=RequestMethod.GET)// means that there is no "create" parameter name in the request and the request method is "GET" to match.
                                                              If the matchable request URL is "http:///*/parameter1?abc"

@ Request Mapping (params = "submitFlag=create", method = RequestMethod. GET)// indicates that there is a "submitFlag=create" request parameter in the request and the request method is "GET" to match.
                                                                        If the request URL is http:///*/parameter2?submitFlag=create

@ Request Mapping (params = "submitFlag=create", method = RequestMethod. POST)// indicates that there is a "submitFlag=create" request parameter in the request and the request method is "POST" to match.

@ Request Mapping (params = "submitFlag=create", method = RequestMethod. GET)// indicates that there is a "submitFlag=create" request parameter in the request and the request method is "GET" to match.
                                                                        If the request URL is http:///*/parameter2?submitFlag=create

Unlike parameter combinations in value, params parameter combinations represent "and", i.e.

@RequestMapping(params={"test1", "test2=create"}) //Represents the presence of“ test1"The parameter name has a“ test2=create"Parameters can be matched, such as matchable requests URL"http://×××/parameter3?test1&test2=create

Posted by simonb on Wed, 26 Jun 2019 12:41:11 -0700