In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use SpringMVC @ RequestMapping annotations". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use SpringMVC @ RequestMapping annotations.
1. @ RequestMapping
Source code for @ RequestMapping annotations:
@ Target ({ElementType.TYPE, ElementType.METHOD}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Mappingpublic @ interface RequestMapping {String name () default "; @ AliasFor (" path ") String [] value () default {}; @ AliasFor (" value ") String [] path () default {}; RequestMethod [] method () default {}; String [] params () default {}; String [] headers () default {}; String [] consumes () default {} Function of String [] produces () default {};} 1.@RequestMapping annotations
The function of @ RequestMapping is to associate the request with the controller method that handles the request to establish a mapping relationship.
When SpringMVC receives the specified request, it will find the corresponding controller method in the mapping relationship to process the request.
Location of 2.@RequestMapping comments
@ RequestMapping annotated in the class: sets the initial information of the mapping request request path (usually used to represent the information of a module)
@ RequestMapping is annotated in the method: set the specific information of the mapping request request path
@ RequestMapping must have only one RequestMapping mapping for the same request address, otherwise an error will be reported.
The browser matches the @ Requestmapping marked on the class first, and then the @ Requestmapping on the method, that is, the request address url = class request path + method request path
@ Controller@RequestMapping ("/ hello") public class DisplayController {@ RequestMapping ("/ displayTest") / / the request at this time is actually / hello/displayTest public String displayTest () {return "display";}}
At this point, the request address to be mapped is actually http://localhost:8080/hello/display.
Visit the display page... Second, @ RequestMapping annotated attribute 1.value attribute (master)
The value attribute matches the request mapping by the request address (default is value). The value attribute is an array of String type, indicating that the request can match the request corresponding to multiple request addresses. The value attribute must be set, at least through the request address mapping to match the request mapping.
/ / indicates that the world.html//@RequestMapping request address can be accessed through either the address testWorld or hello/testWorld, which has nothing to do with the method name, but in order to facilitate the habitual use of the method name @ RequestMapping (value = {"testWorld", "hello/testWorld"}) public String testWorld () {return "world";}
The request address that needs to be mapped is actually http://localhost:8080/testWorld or http://localhost:8080/hello/testWorld.
@ {/ hello/testWorld} visit the helloworld page.
@ {/ testWorld} visit the helloworld page.
2.method attributes (master)
Method matches the request mapping through the request method (get/post). The method attribute is an array of RequestMethod [] type, indicating that this kind of request can match requests corresponding to multiple request methods. If the request mapping of the current request satisfies the value attribute but not the method attribute, an error of 405 will be reported: Request method 'POST' not supported
What is the difference between the two common request methods: get and post?
Get: use? Concatenate the request parameter with the request address in the format:? Request parameter name = request parameter value & request parameter name = request parameter value, insecure, fast transfer speed (request address will also be transferred), limited amount of data transfer, can not be used to upload files
Post: request parameters are placed in the request body (format: request parameter name = request parameter value & request parameter name = request parameter value), security, slow transmission speed and large amount of data
/ / indicates that world.html@RequestMapping (value = {"testWorld", "hello/testWorld"}, method = {RequestMethod.GET,RequestMethod.POST}) public String testWorld () {return "world";} @ {/ testWorld} can be accessed via either testWorld or hello/testWorld (GET request (default))
The request for accessing get is normal. The request for accessing post is mismatched (the request is not supported). Error 405 is reported.
Change the @ RequestMapping of WorldController to @ RequestMapping (value = {"testWorld", "hello/testWorld"}, method = {RequestMethod.GET,RequestMethod.POST}), visit the post and get request again, and the request is successful.
3.params property (understand)
The params attribute refers to the mapping that handles the request through the matching of the requested parameters, and the params attribute is an array of String types that represents the matching relationship between the request parameters and the request mapping set by four expressions.
"param": requires that the request matched by the request map must carry param request parameters
"! param": requests matched by request mappings must not carry param request parameters
"param=value": requires that the request matched by the request map must carry param request parameters and param=value
"paramorphic required value": requires that the request matched by the request map must carry the param request parameter but the paramedical required value
/ / the parameters for processing requests can only be username and password, and username=admin and password=12345678@RequestMapping (value = "/ testParams", params = {"username=admin", "password=12345678"}) public String testParams () {return "world";} Test params parameters
Can you see that the request parameters have been used? Spliced, the request for access is successful
Change the request to the following, visit again, @ RequestMapping request parameters do not match, report 400
Test the params parameter root
4.headers property (understand)
The headers attribute matches the request mapping through the request header information, and the headers attribute is an array of String types that indicates that the request header information and the request mapping match information can be set through four expressions.
"header": requests matched by request mapping must carry header request header information
"! header": requests matched by request mapping must not carry header request header information
"header=value": requests matched by request mapping must carry header request header information and header=value
"headermapped value": requires that the request matched by the request map must carry the header request header information and the headerqualified value
If the current request satisfies the value and method attributes of the @ RequestMapping annotation, but not the headers attribute, the page displays a 404 error, that is, the resource was not found
/ / username=admin,password=1234545678 in the params of the request at this time, and the port number is 8081 before it can be successful, otherwise @ RequestMapping (value = "/ testParams", params = {"username=admin", "password=12345678"}, headers = {"host=localhost:8081"}) public String testParams () {return "world";} Test params parameter
You can see that it is not found. The request header parameters of @ RequestMapping do not match. Error 404 is reported.
Change the port number to 8080 and test it again.
/ / username=admin,password=1234545678 in the params of the request at this time, and the port number is 8080 before it can be successful, otherwise @ RequestMapping (value = "/ testParams", params = {"username=admin", "password=12345678"}, headers = {"host=localhost:8080"}) public String testParams () {return "world";}
You can see that the visit was successful.
Summary:
@ RequestMapping does not match any parameters, error 404 is reported
@ RequestMapping request header parameter mismatch, error 404
@ RequestMapping request parameters (params) mismatch, error 400 reported
@ RequestMapping request method does not match (request is not supported), error 405
5.SpringMVC supports ant-style paths
?: represents any single character
*: represents any 0 or more characters
* *: indicates any one-or multi-tier directory
Note: when using * *, only / * * / xxx can be used.
/ / @ RequestMapping (value = "/ a?a/testAnt") / / @ RequestMapping (value = "/ a*a/testAnt") @ RequestMapping (value = "/ * / testAnt") public String testAnt () {return "world";} Test ant style-- >?
Test ant style-> *
Test ant style-> * *
6.SpringMVC supports placeholders in paths (emphasis)
Original method: / deleteUser?id=1
Rest mode:: / deleteUser/1
The placeholder in the SpringMVC path is often used in the restful style. When some data is transferred to the server through the path in the request path, the transferred data can be represented by the placeholder {xxx} in the value attribute of the corresponding @ RequestMapping annotation, and the data represented by the placeholder can be assigned to the formal parameters of the controller method through the @ PathVariable annotation.
@ RequestMapping ("/ testRest/ {username} / {password}") public String testRest (@ PathVariable ("username") String username,@PathVariable ("password") String password) {System.out.println ("username=" + username); System.out.println ("password=" + password); return "world";} Test rest placeholder
Can you see that it is not used? Connection, using the rest way to achieve access.
Look at the console of idea, and the data is transferred to the server by path.
Third, @ RequestMapping's derived class comments
For controller methods that handle specified requests, SpringMVC provides a derived class annotation for @ RequestMapping:
Mapping for handling post requests-> @ PostMapping
Mapping for handling get requests-> @ GetMapping
Mapping for handling put requests-> @ PutMapping
Mapping for handling delete requests-> @ DeleteMapping
But at present, browsers only support get and post. If a string of other request methods (put or delete) is set for method when the form form is submitted, it will be handled according to the default request method (get).
@ GetMapping ("testGetMapping") public String testGetMapping () {return "world";} @ {/ testGetMapping} visit the helloworld page.
Test whether the form form supports requests in put or delete mode
An error of 405 will be reported when the request and request mapping do not match.
/ / the request address mapped by the current request is testPut, and the request method is put @ RequestMapping (value = "/ testPut", method = RequestMethod.PUT) public String testPut () {return "world";}
The results are as follows:
If a string of other request methods (put or delete) is set for method when the form form is submitted, the default request method (get) will be followed. To send put and delete requests, you need to use the filter HiddenHttpMethodFilter provided by spring, which is discussed in the restful section.
Thank you for reading, the above is the content of "how to use SpringMVC @ RequestMapping annotations". After the study of this article, I believe you have a deeper understanding of how to use SpringMVC @ RequestMapping annotations, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.