SPRING in ACTION 4th Edition notes-seventh chapter advanced Spring mvc-006-How to maintain redirected request data (with model, placeholder, redirectattributes)

Source: Internet
Author: User

Why is redirect losing data?

When a handler method completes, any model data specified in the method was copied into the request as request attributes, and the request was for warded to the view for rendering. Because it ' s the same request that's handled by both

the Controller method and the view, the request attributes survive the forward.but as illustrated in Figure 7.1, when a controller method is results in a redirect, theoriginal request ends and a new HTTP GET request begins. Any model data carried inThe original request dies with the request. The new request is devoid of any modelThe data in its attributes and have to the figure of it out on its own.Clearly, the model isn ' t going to help you carry data across a redirect. But there is
a couple of options to get the data from the redirecting method to the redirectHandling method:
? Passing data as path variables and/or query parameters using URL templates
? sending data in Flash attributes

Two, the spelling URL string

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

Iii. using model and placeholders

By adding the model parameter to the method, the data in the model can match the placeholder as the path parameter, otherwise it will be used as the query parameter, as

 1  @RequestMapping (value = "/register", method =  POST)  2  public   String processregistration ( 3   Spitter Spitter, model model) { 4   Spitte Rrepository.save (spitter);  5  Model.addattribute ("username" , Spitter.getusername ());  6  Model.addattribute ("Spitterid" , Spitter.getid ());  7  return  "Redirect:/spitter/{username} " 8 } 

The placeholder encodes the string, and it's safe to stitch it directly, Because It ' s filled into the placeholder in the URL template instead of concatenated

into the redirect String, any unsafe characters in the Username property is escaped. This is safer than allowing the user into type in whatever they want for the username and Then appending it to the path.

because the ID does not match the placeholder, Suppose username attribute is Habuma and the Spitterid attribute are, then the resulting redirect path would be/spitter/habuma?spitterid=42

Third, with Flash attributes->redirectattributes

1. Requirements:

Let's say that instead of sending a username or ID in the redirect and you want to send the actual Spitter object. If you send a just the ID, then the method that handles the Redirect have to turn around and look up thespitter fr Om the database. But before the redirect, you already has the Spitter object in hand. Why isn't send it to the redirect-handling method to display?

2.

redirectattributes is the sub-interface of the model and works with the session, before the redirect takes place, all flash attributes is copied into the session. after  into the model. The method that handles the redirect request can then access the  spitter from the model, just Like any other model object. Figure 7.2 illustrates how  this Works.

3. Code implementation

 1  @RequestMapping (value = "/register", method =  POST)  2  public   String processregistration ( 3   Spitter Spitter, redirectattributes model) { 4   Spitterrepository.save (spitter);  5  Model.addattribute ("username" , Spitter.getusername ());  6  Model.addflashattribute ("Spitter" , Spitter) ; 7  return  "Redirect:/spitter/{username} " 8 } 

1@RequestMapping (value = "/{username}", method =GET)2  PublicString Showspitterprofile (3 @PathVariable String username, model model) {4     if(!model.containsattribute ("Spitter")) {5 Model.addattribute (6 Spitterrepository.findbyusername (username));7     }8     return"Profile";9}

SPRING in ACTION 4th Edition notes-seventh chapter advanced Spring mvc-006-How to maintain redirected request data (with model, placeholder, redirectattributes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.