SpringMVC -- Request ing and springmvc ing
In SpringMVC, how to handle requests is an important task. All request mappings are marked with @ RequestMapping. The annotation on the class is equivalent to a prefix, indicating that the processor processes requests of the same class; the Annotation on the method is more detailed. For example, the annotation of a class may be "user", indicating that all operations are related to users; specific methods may include "create", "update", and "delete, which operation is performed on the user.
Package cn. javass. chapter6.web. controller; @ Controller @ RequestMapping (value = "/user") // ① general ing prefix of the processor public class HelloWorldController2 {@ RequestMapping (value = "/hello2 ") // ② narrow the public ing relative to ① public ModelAndView helloWorld () {// omitted implement }}
Now let's summarize the request mappings.
1. URL path ing
Only value is involved in this ing.
@ RequestMapping (value = {"/test1", "/user/create"}) // or, it indicates that multiple paths can be mapped to the same processing method @ RequestMapping (value = "/users/{userId}/topics/{topicId}") // you can also use braces, variable placeholder @ RequestMapping (value = "/product? ") // Matches"/product1 "or"/producta ", but does not match "/product" or "/productaa" @ RequestMapping (value = "/products/**/{productId }") // match "/products/abc/123" or "/products/123" @ RequestMapping (value = "/products/{categoryCode: \ d +}-{pageNumber: \ d +} ") // supports regular expressions.
Ii. Restrictions on Request Method ing
Not only the value attribute, but also the method attribute.
@ RequestMapping (value = "/create", method = RequestMethod. GET) // indicates that the request that matches "/create" and the request method is "GET" can be processed @ RequestMapping (value = "/create", method = RequestMethod. POST) // indicates that the request that matches "/create" and the request method is "POST" can be processed.
Generally, browsers only support the GET and POST types, and Other types such as PUT and DELETE need to be simulated.
Iii. Request Parameter ing restrictions
The params and method attributes must be provided.
The following controller is used as an example,
@ Controller @ RequestMapping ("/parameter1") // ① general ing prefix of the processor public class RequestParameterController1 {// Something ...}
@ RequestMapping (params = "create", method = RequestMethod. GET) // indicates that the request has a "create" parameter name and the request method is "GET". For example, the request URL "http: // ×××/parameter1? Create "@ RequestMapping (params =" create ", method = RequestMethod. POST) // indicates that the request has a "create" parameter name and the request method is "POST". Then, @ RequestMapping (params = "! Create ", method = RequestMethod. GET) // indicates that the request does not contain the "create" parameter name and the request method is "GET". For example, the request URL "http: // ×××/parameter1? Abc "@ RequestMapping (params =" submitFlag = create ", method = RequestMethod. GET) // indicates that the request contains the "submitFlag = create" request parameter and the request method is "GET". For example, the request URL is http: // ×××/parameter2? SubmitFlag = create @ RequestMapping (params = "submitFlag = create", method = RequestMethod. POST) // indicates that the request contains the "submitFlag = create" request parameter and the request method is "POST" to match @ RequestMapping (params = "submitFlag = create", method = RequestMethod. GET) // indicates that the request contains the "submitFlag = create" request parameter and the request method is "GET". For example, the request URL is http: // ×××/parameter2? SubmitFlag = create
Different from the parameter combination in value, the params parameter combination indicates "and", that is:
@ RequestMapping (params = {"test1", "test2 = create"}) // indicates that the request has the "test1" parameter name and the "test2 = create" parameter to match, if the matching request URL is "http: // ××/parameter3? Test1 & test2 = create