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

How SpringMVC redirects how data is carried in redirect requests

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

Share

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

This article is about how Spring MVC redirects the way data is carried in redirect requests. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

SpringMVC redirect request carries data

The redirect: prefix is used in the view name returned by the controller method. In this case, the String returned is not used to find the view, but the path to which the browser jumps:

return "redirect:/spitter/" + spitter.getUsername();

When a controller returns a redirect, the original request terminates and a new HTTP request is opened. All model data in the request will be cleared. The new request will not have any model data, as shown in the figure:

It is no longer possible to use model to pass data when redirecting. But there are other ways to get data from redirected methods:

Convert data to path parameters or query parameters

Sending data in flash attributes First let's look at how Spring passes data in path parameters or query parameters.

@RequestMapping(value="/register", method=POST)public String processRegistration(Spitter spitter, Model model) { spitterRepository.save(spitter); model.addAttribute("username", spitter.getUsername()); return "redirect:/spitter/{username}";}@RequestMapping(value = "/{username}", method = RequestMethod.GET) public String showSpitterProfile(@PathVariable String username, Model model) { System.out.println("showSpitterProfile"); Spitter spitter = spittleRepository.findByUsername(username); model.addAttribute(spitter); return "Profile"; }

Other original values in the model are also added to the redirect URL as query parameters. For example, in addition to username, model also includes the id attribute of the newly created Spitter object:

@RequestMapping(value="/register", method=POST)public String processRegistration(Spitter spitter, Model model) { spitterRepository.save(spitter); model.addAttribute("username", spitter.getUsername()); model.addAttribute("spitterId", spitter.getId()); return "redirect:/spitter/{username}";}

However, since the spitterId attribute in the model does not map to a placeholder in the URL, it is automatically used as a query parameter.

If username is habuma and spitterId is 42, the redirect path returned would be/spitter/habuma? spitterId=42。

Use flash properties

Spring provides RedirectAttributes to set flash attributes, RedirectAttributes as a sub-interface of Model, and new methods to set flash attributes.

@RequestMapping(value="/register", method=POST)public String processRegistration(Spitter spitter, RedirectAttributes model) { spitterRepository.save(spitter); model.addAttribute("username", spitter.getUsername()); model.addFlashAttribute("spitter", spitter); return "redirect:/spitter/{username}";}

All flash attributes are copied into the session before the redirection is performed. After redirection, flash attributes existing in the session are removed and transferred from the session to the model. The method that handles redirects can access Spitter objects from the model just like any other model object.

@RequestMapping(value = "/{username}", method = RequestMethod.GET)public String showSpitterProfile(@PathVariable("username") String username, Model model) { if (! model.containsAttribute("spitter")) { Spitter spitter = spitterRepository.findByUsername(username); model.addAttribute(spitter); } return "profile";}

The first thing the showSpitterProfile() method does is check to see if there is a model attribute with key spitter. If the spitter attribute is included in the model, then nothing needs to be done. The Spitter object contained in this will be passed to the view for rendering. However, if the model does not contain spitter attributes, showSpitterProfile() will look up the Spitter from the Repository and store it in the model.

Redirect Display Data

................ username:firstName:lastName: email:.... SpringMVC several redirection methods to carry data 1. concatenation string return "redirect:/page/second? param1=lay¶m2=lay2";2. Use RedirectAttribute to pass parameters public String first(RedirectAttribute redirectAttribute){ redirectAttribute.addAttribute("param1", "lay"); return "redirect:/page/second";}3.RedirectAttribute--addFlashAttribute() Usage @Controller@RequestMapping("/page")public class redirectDemo{ @RequestMapping("/first") public String first(RedirectAttribute redirectAttribute){ redirectAttribute.addFlashAttibute("param1", "lay"); return "redirect:/page/second"; } @RequestMapping("/second") public String second(@ModelAttribute("param1") String param1){ System.out.println(param1); return "second"; Thank you for reading! About "SpringMVC how to redirect the way to carry data in the request" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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