In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the introduction of Spring MVC redirection and forwarding and the difference between redirect and forward". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "the introduction of Spring MVC redirection and forwarding and the difference between redirect and forward" together!
directory
redirect and forward
Common processing methods
String redirection
ModelAndView redirect
RedirectView redirect
Jump with parameters
Difference Between Forward and Redirect
redirect and forward
Redirects go through the client and forwarding doesn't, so forwarding is relatively fast. However, sometimes it is easier to use redirection, such as:
Redirect to external websites;
Avoid invoking the same action again when users reload the page.
return "redirect:/view/"+saveUser.getId();
Redirection is used here to prevent 'saveUser' from being called twice when the current user reloads the page.
However, redirection does not easily pass values to the target page, so Flash attributes were provided after Spring 3.1, as described below.
Common processing methods
Jumps between Controller view methods are nothing more than jumps with parameters and jumps without parameters. Common methods include String mapping RequestMapping to achieve redirection, or ModelAndView object, or RedirectView object, described below one by one.
String redirection
return is a string that maps to another Controller method. If there are request parameters, they are concatenated after the RequestMapping string.
//return string mapping method @RequestMapping("hello")public String hello(HttpServletRequest req, HttpServletResponse resp) { doSomething(); return "redirect:/bye"; // return "redirect:/bye? username=sudoz";}ModelAndView redirect
Another way to do this is by returning a ModelAndView object. Similarly, if there are request parameters, you can also use a method similar to GET parameter concatenation:
//return ModelAndView object @RequestMapping("hello")public ModelAndView hello(HttpServletRequest req, HttpServletResponse resp) { doSomething(); return new ModelAndView("redirect:/bye"); // return new ModelAndView("redirect:/bye? username=sudoz");}RedirectView redirection
There is also a way to jump by returning a RedirectView object. This method differs from the above in that the RedirectView object does not need to set the redirect prefix:
//return RedirectView object @RequestMapping("hello")public RedirectView hello() { doSomething(); return new RedirectView("/bye"); // return new RedirectView("bye? username=sudoz");} Jump with parameters
Model loses messages when redirected
When doing method jumps, it is sometimes not practical to pass parameters to the next method by concatenating URL parameters, as in the code above. Because parameters are not necessarily strings, and browsers have limits on the length of URLs. The RedirectAttributes object can be used to hold parameters when requesting redirection. Rewrite the above code with RedirectAttributes:
@RequestMapping("/")public RedirectView hello(RedirectAttributes attrs) { attrs.addAttribute("message", "hello"); attrs.addFlashAttribute("username", "world"); return new RedirectView("hello");}@RequestMapping("hello") Map hello(@ModelAttribute("message") String meaasge, @ModelAttribute("username") String username) { Map map = new HashMap(); map.put("message", message); map.put("username", username); return map;}
In the above code, the addAttribute() and addFlashAttribute() methods are called to insert two values into the RedirectAttributes object. If you look at the source code, you can know that the implementation class of the RedirectAttributes interface RedirectAttributesModelMap inherits ModelMap, which is essentially a subclass of HashMap, so it can be used to store Key-Value pairs. These two methods are very common, and there must be differences in their use:
The addAttribute() method adds Key-Value to the URL as a request parameter;
FlashaddAttribute () method will temporarily store Key-Value in session, after jumping to the target method, that is, complete the task, will be deleted from the session;
Use the curl command to test:
curl -i http://localhost:8080/HTTP/1.1 302 Set-Cookie: JSESSIONID=D1CC5E15FA8EF9474C4CED7D4F660E66;path=/;HttpOnlyLocation: http://localhost:8080/hello;jsessionid=D1CC5E15FA8EF9474C4CED7D4F660E66? username=sudozContent-Language: en-USContent-Length: 0Date: Thu, 16 Feb 2017 12:33:46 GMT
As you can see, the key-value pairs added by addAttribute() become parameters after the URL, and the key-value pairs added by addFlashAttribute() do not appear on the URL, but are stored in the session. The target method of the jump specifies the parameters received with the @ModelAttribute("key") annotation.
Difference Between Forward and Redirect
The three methods listed above are actually the redirection of Spring MVC when processing requests, that is, redirect jump. Another way to distribute requests is forwarding, or forward. The difference between the two is clear from the HTTP specification:
The HTTP return code for redirect is 302, and the new URL to which it is redirected is stored in the Location field of HTTP Response Headers. After receiving the Response, the client will initiate another request. The URL of this request is the redirected URL.
Forward forwarding occurs only on the server side; the Servlet container sends the source request directly to the target URL, not via the client; therefore, the client receives the response from the forwarded target method, but the URL rendered by the browser does not change, and forward cannot forward the parameters.
Thank you for reading, the above is "Spring MVC redirection and forwarding introduction and the difference between redirect and forward" content, after learning this article, I believe that everyone on Spring MVC redirection and forwarding introduction and the difference between redirect and forward this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.