@RequestParam, you must have seen it; @PathVariable, you must have known it; @QueryParam, why don't you know it? ! And he is familiar to you (@CookieValue)! She (@ModelAndView)! It (@ModelAttribute)! That’s right, just annotating this, spring mvc opens up a colorful world for you. Come on, don't be excited (mi) and (hu), sit down, let's have a good chat about these annotations brothers~~~(wait, haven't you heard of it? Okay, come on, you sit in the front row, you'll be there!)
1. How to match the request path in spring mvc - "Which request path is better, RequestMapping is well-known"
@RequestMapping is used to map requests, such as get requests, post requests, or REST styles and non-REST styles. This annotation can be used on a class or on a method, if used on a class, representing the parent path of all methods in the class.
For example (the test classes used here such as SpringMVCTest and some pages have been introduced in the first article "Learning SpringMVC - Starting from HelloWorld"):
Add test methods to SpringMVCTest.java:
@RequestMapping("/testRequestMapping")public String testRequestMapping(){ System.out.println("testRequestMapping"); return SUCCESS;}Note that the annotation @RequestMapping ("/testRequestMapping") is added to the method level, which means that this method can be located through the relative path of "/testRequestMapping". At the same time, we also put a class-level RequestMapping annotation on the SpringMVCTest class:
@RequestMapping("/springmvc")@Controllerpublic class SpringMVCTest {Note that there is also an annotation of @Controller. This annotation is responsible for processing requests distributed by the DispatcherServlet in SpringMVC. It encapsulates the data requested by the user through the service processing layer and then returns the model to the corresponding View for display. At this point, we have a path like "springmvc/testRequestMapping", and then we can locate the testRequestMapping method and then execute the method body within the method.
To add one more thing, RequestMapping can implement fuzzy matching paths, such as:
? : Match a character
*: Match any character
**: Match multi-layer paths
/springmvc/**/lastTest can match paths like /springmvc/firstTest/secondTest/lastTest
2. How to obtain the requested parameters of spring mvc - "Eight Immortals cross the sea, each showing their magical powers"
1. @PathVariable
This annotation is used to map the placeholders bound in the request URL. Through @PathVariable, you can bind the parameter of the placeholder in the URL to the parameter of the controller processing method. Don't you understand? See example:
@RequestMapping("/testPathVariable/{id}")public String testPathVariable(@PathVariable(value="id") Integer id){ System.out.println("testPathVariable:" + id); return SUCCESS;}In index.jsp we add a connection to trigger a request:
<a href="springmvc/testPathVariable/1">testPathVariable</a><br/><br/>
We can see that there is a hyperlink here. After clicking, we will enter the controller processing method corresponding to springmvc/testPathVariable/1. We now want to get the "1" in this request parameter, so add "/testPathVariable/id" to the testPathVariable method, about id", and the specific correspondence of {id}. In the parameters of this method, @PathVariable(value="id") is declared, and bind and receive it through Integer id. Through this method, we can get the parameter "1" requested by the front-end page.
2. @RequestParam
This annotation is also used to obtain the request parameters. So how is this annotation different from @PathVariable? Let's look at the example:
Adding methods in SpringMVCTest
@RequestMapping(value="/testRequestParam")public String testRequestParam(@RequestParam(value="username") String username, @RequestParam(value="age", required=false, defaultValue="0") int age){ System.out.println("testRequestParam" + " username:" + username + " age:" +age); return SUCCESS;}Add hyperlink tag in index.jsp
<a href="springmvc/testRequestParam?username=jackie&age=12">testRequestParam</a><br/><br/>
Clicking on the hyperlink on the page will match the path of RequestMapping on the testRequestParam method in the controller. Note that in this method, we declare two variables through the annotation @RequestParam to obtain the parameter value brought by query in the request, one is the value after username and the other is the value after age.
After seeing this, you probably understand some of the differences between @PathVariable and @RequestParam. For requests like "springmvc/testPathVariable/1", we use @PathVariable to bind the requested parameters; while for request parameters like "springmvc/testRequestParam?username=jackie&age=12" appear as key-value pairs. We use @RequestParam to obtain the specific request value after username or age.
QueryParam is also similar to RequestParam, because it is not an annotation within the spring mvc framework, so it will not be described in detail here.
For different request types and request methods, spring mvc has a set of targeted solutions. Let’s take a look at what the popular REST style requests are like now - using REST style to achieve addition, deletion, modification and search.
The bottom-up implementation of the interface for checking (get), adding (post), deleting (delete) and modifying (put) is implemented in the SpringMVCTest class from the bottom up
@RequestMapping(value="/testRest/{id}", method=RequestMethod.PUT)public String testRestPut(@PathVariable(value="id") Integer id){ System.out.println("test put:" + id); return SUCCESS;} @RequestMapping(value="/testRest/{id}", method=RequestMethod.DELETE)public String testRestDelete(@PathVariable(value="id") Integer id){ System.out.println("test delete:" + id); return SUCCESS;} @RequestMapping(value="/testRest", method=RequestMethod.POST)public String testRest(){ System.out.println("test post"); return SUCCESS;} @RequestMapping(value="/testRest/{id}", method=RequestMethod.GET)public String testRest(@PathVariable(value="id") Integer id){ System.out.println("test get:" + id); return SUCCESS;}So how to implement the front desk interface? The corresponding order is
<form action="springmvc/testRest/1" method="post"> <input type="hidden" name="_method" value= "PUT"/> <input type="submit" value="testRestPut"/></form><br/> <form action="springmvc/testRest/1" method="post"> <input type="hidden" name="_method" value="DELETE"/> <input type="submit" value="TestRest DELETE"/></form><br><br> <form action="springmvc/testRest" method="post"> <input type="submit" value="testRestPost"></form><br/><br/> <a href="springmvc/testRest/1">testRest</a><br/><br/>
In addition, we also need to add a statement in the configuration file web.xml that supports converting post to delete and put requests.
<!-- Configure HiddenHttpMethodFilter: You can convert POST requests to DELETE or POST requests--><filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
As you can see, the modification and deletion here are sent through post, because put and delete are not supported here to directly implement deletion and modification. Instead, by using the post method and quietly bringing a token hidden type input tag to tell the background that what I sent in the front desk is actually a request for deletion and modification.
So how does this process be implemented, why add
<input type="hidden" name="_method" value="DELETE"/>
This token will be bought by others in the background. Then let’s see how we bought it later.
In the final analysis, it is due to the addition of the HiddenHttpMethodFilter class in web.xml. There is a method doFilterInternal in this class. Through debugging, we can find clues, start tomcat (not tomcat8), click the corresponding input tag of the delete operation, and enter the debugging interface. We can see:
Small pit: Here, please note that the startup cannot be tomcat8, but can only be a smaller version than 8, such as 7 or 6, etc. The following figure shows the error message using tomcat and the successful response using 7:
To summarize, how to send a request for put and delete:
1. Configure HiddenHttpMethodFilter in web.xml
2. Send post request
3. There is a hidden domain in the request, name is "_mothod", value is put or delete
Finally, let’s talk about the annotation @CookieValue.
3. @CookieValue
This annotation is also a similar routine, and it is also a mapping, which maps a cookie value.
When we send a request, we can see that the request is carrying some cookie values.
For example, JSESSIONID or Path here. Now we will write a method to get the cookie value.
Add in SpringMVCTest
@RequestMapping(value="/testCookieValue")public String testCookieValue(@CookieValue("JSESSIONID") String cookieValue){ System.out.println("testCookieValue: " + cookieValue); return SUCCESS;}Add link to the index.jsp interface
<a href="springmvc/testCookieValue">testCookieValue</a><br/><br/>
In this way, we can get results like "testCookieValue: 1410F05C9ADD84E8659C2AC79E8CC666".
So far, we've introduced
1. Usage of @RequestMapping
2. Get the usage of @PathVariable and @RequestParam of the request parameters
3. Introduce how to implement REST-style requests, and analyze how post is converted into delete and put requests
4. Introduction to the usage of @CookieValue
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.