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 to redirect springboot to carry data RedirectAttributes

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

Share

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

This article introduces you how to redirect springboot to carry data RedirectAttributes, the content is very detailed, interested friends can refer to, hope to be helpful to you.

How do I carry data when the controller layer needs to be redirected to a specified page?

Traditional use of session

Use RedirectAttributes. (using session principle)

Advantages: addFlashAttribute and other methods are provided. Ensure that the data can only be used once and then deleted

Use of RedirectAttributes public interface RedirectAttributes extends Model {RedirectAttributes addAttribute (String var1, @ Nullable Object var2); RedirectAttributes addAttribute (Object var1); RedirectAttributes addAllAttributes (Collection var1); RedirectAttributes mergeAttributes (Map var1); RedirectAttributes addFlashAttribute (String var1, @ Nullable Object var2); RedirectAttributes addFlashAttribute (Object var1); Map getFlashAttributes ();}

Add RedirectAttributes. RedirectAttributes directly to the parameters of Controller.

AddFlashAttribute will delete the data in session after redirecting to the next page to retrieve the data.

The addFlashAttribute method stores the data in session and is invalidated after one access.

@ PostMapping ("/ regist") public String register (RedirectAttributes attribdatautes) {int data = 1; attributes.addFlashAttribute ("data", data); return "redirect: http://auth.gulimail.com/reg.html";}

The addAttribute method splices the data after url (in the form of get)

@ GetMapping ("/ addToCartSuccess.html") public String addToCartSuccessPagez (@ RequestParam ("skuId") Long skuId,Model model) {CartItem cartItem = cartService.selectCartItemInfo (skuId); model.addAttribute ("item", cartItem); return "success";} RedirectAttributes cannot be read after storing the value

First, check whether the Controller is @ Controller or @ RestController (the difference between the two is Baidu)

Secondly, the following

@ GetMapping ("/ redirect") public String redirect (RedirectAttributes redirectAttributes) {redirectAttributes.addFlashAttribute ("test", 1); return "redirect:/show";} @ GetMapping ("/ show") @ ResponseBody// must add the @ ModelAttribute tag, otherwise the side will not read the value / / and must specify the variable name, and will not automatically match public Map show (@ ModelAttribute ("test") int test) {Map modelMap = new HashMap (); modelMap.put ("String", test); return modelMap } this is the end of RedirectAttributes sharing on how to redirect springboot to carry data. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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