Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Spring MVC redirection and forwarding

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "what is Spring MVC redirection and forwarding". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is Spring MVC redirection and forwarding"?

Spring MVC redirect and forward redirect and forward

Redirection passes through the client, but forwarding does not, so forwarding is relatively faster. But sometimes it is more convenient to use redirection, such as:

Redirect to an external website

Avoid calling the same action again when the user reloads the page.

Return "redirect:/view/" + saveUser.getId ()

Redirection is used here to prevent''saveUser''' from being called again when the current user reloads the page.

However, it is not easy to pass a value to the target page using redirection, so the Flash property is provided after Spring3.1, as detailed later.

Common treatment methods

The jump between Controller view methods is nothing more than jump with and without participation. Common methods are redirection through String mapping RequestMapping, or through ModelAndView object, or RedirectView object, which is explained one by one below.

String redirection

Is a string that return maps to another Controller method. If there is a request parameter, it is concatenated after the string mapped by RequestMapping.

/ / return string mapping method @ RequestMapping ("hello") public String hello (HttpServletRequest req, HttpServletResponse resp) {doSomething (); return "redirect:/bye"; / / return "redirect:/bye?username=sudoz";} ModelAndView redirection

Another way is to jump by returning a ModelAndView object. Similarly, if you have 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

Another way is to jump by returning a RedirectView object, which 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 will lose messages when it is redirected.

When doing a method jump, if you want to bring parameters to the next method, like the method in the above code by concatenating URL parameters is sometimes not practical. Because parameters are not necessarily strings, and browsers have limits on the length of URL. The RedirectAttributes object can be used to hold the parameters when the request is redirected. Rewrite the above code using 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, two values are inserted into the RedirectAttributes object by calling the addAttribute () and addFlashAttribute () methods. If you look at the source code, you can see that the implementation class RedirectAttributesModelMap of the RedirectAttributes interface inherits ModelMap and is essentially a subclass of HashMap, so it can be used to store Key-Value pairs. Both methods are commonly used, and there are bound to be differences in use:

The addAttribute () method takes Key-Value as a request parameter after the added URL

The addFlashAttribute () method temporarily stores the Key-Value in the session. After jumping to the target method, the task is completed and deleted from the session.

Test with the curl command:

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 pair added by addAttribute () becomes the parameter after URL, while the key-value pair added by the addFlashAttribute () method does not appear on the URL, but is stored in the session. The target method of the jump specifies the received parameters through the @ ModelAttribute ("key") annotation.

The difference between redirect and forward

The three methods listed above are actually the redirection of Spring MVC when processing the request, that is, redirect jump. Another way to distribute requests is to forward, or forward. The difference between the two is clear from the HTTP specification:

The HTTP return code of redirect is 302, and the new URL of the jump is stored in the Location field of HTTP Response Headers. After receiving the Response, the client initiates another request. The URL of this request is the redirected URL.

The forwarding process of forward only occurs on the server side; the Servlet container directly calls the source request to the destination URL, rather than initiating the request through the client; therefore, the response received by the client comes from the forwarded destination method, but the URL rendered by the browser does not change, and the forward cannot forward the parameters.

At this point, I believe you have a deeper understanding of "what is Spring MVC redirection and forwarding". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report