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

Example Analysis of SpringMVC redirection Parameter RedirectAttributes

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

Share

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

This article mainly introduces the example analysis of SpringMVC redirection parameter RedirectAttributes, which is very detailed and has certain reference value. Friends who are interested must read it!

Redirect parameter RedirectAttributes

Redirect is often used in SpringMVC to achieve redirection. However, each scenario has its own requirements, and a simple page jump obviously cannot meet all the requirements, such as stitching parameters in url when redirecting, or passing Model to the returned page.

SpringMVC addresses these two needs with RedirectAttributes.

1. AddAttribute@RequestMapping ("/ save") public String save (User user, RedirectAttributes redirectAttributes) {redirectAttributes.addAttribute ("param", "value1"); return "redirect:/index";}

After requesting / save, jump to / index and splice? param=value1 in url.

2. AddFlashAttribute@RequestMapping ("/ save") public String save (User user, RedirectAttributes redirectAttributes) {redirectAttributes.addFlashAttribute ("param", "value1"); return "redirect:/index";}

After requesting / save, jump to / index, and you can get the return value through expressions in the template corresponding to index, for example, jstl uses ${param} in jsp. This value is actually saved in session and will be deleted the next time the request is redirected.

This is the brief introduction to the two methods in RedirectAttributes.

Description of the problem of redirect carrying parameters

A.jsp sends a request to enter Controller and wants to redirect to B.jsp and carry parameters. It is found that the parameters carried cannot be obtained by the foreground, and then the following methods can be used.

@ RequestMapping ("/ index") public String delete (String id, RedirectAttributes redirectAttributes) {redirectAttributes.addFlashAttribute ("msg", "deleted successfully!") ; return "redirect:hello";} @ RequestMapping ("hello") public String index (@ ModelAttribute ("msg") String msg) {return "sentinel";}

First go to the delete method, put msg in redirectAttributes, then redirect to hello, get the value of msg through @ ModelAttribute ("msg") String msg, then the natural sentinel page can get the value of msg.

Source of problem

B.jsp sends the request, jumps to A.jsp, and carries the data generated by the request to page A.

The above is all the contents of the article "sample Analysis of SpringMVC Redirection Parameter RedirectAttributes". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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