Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedirectAttributes Flash scoped variables absent when redirecting using ResponseEntity [SPR-15176] #19742

Closed
spring-projects-issues opened this issue Jan 23, 2017 · 4 comments
Assignees
Labels
in: web type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jan 23, 2017

Edward Smith opened SPR-15176 and commented

In short, when Flash scoped attributes are added in a @Controller method and then a redirect is executed by returning a type of ResponseEntity with an HTTP status of 302 Found, the Flash scoped attributes are lost.

See https://github.com/tednology/SPR-15176 for a working project demonstrating the bug.

@Controller
public class DemoController {
    
    @GetMapping("/bad")
    ResponseEntity<Void> bad(RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("message", "Bad!");
        return ResponseEntity.status(HttpStatus.FOUND)
            .location(URI.create("/"))
            .build();
    }

    @GetMapping("/good")
    String good(RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("message", "Good!");
        return "redirect:/";
    }

    @GetMapping("/")
    String home(Model model) {
        // Will print flash scoped message "Good!", but not "Bad!"
        model.asMap().entrySet().forEach(System.err::println);
        return "home";
    }
}

Affects: 4.3.4

Reference URL: https://github.com/tednology/SPR-15176

Referenced from: commits 5ea5f81

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 23, 2017

Rossen Stoyanchev commented

RedirectAttributes, and also model attributes, are used when rendering through view resolution with a view and a model. Support for flash attributes was exposed through RedirectAttributes because it fits the redirect scenario but also because flash scope commonly comes up in browser applications for displaying a message after a post, etc. REST controllers tend to be more stateless.

So technically not a bug but rather an enhancement request. It shouldn't be hard to add. It's just a thin layer over the underlying FlashMapManager. That said I'm curious if you could share a little more about your use case.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 23, 2017

Edward Smith commented

Sure thing - I have written a helper library I used to manage messages of various severity levels (e.g. INFO / WARNING / ERROR / SUCCESS), in HTML views as you mentioned. Naturally I use redirect attributes / flash scope to preserve these messages across redirects.

I had a Controller class with mixed View rendering (e.g. String return types) and some REST-ish methods handling AJAX calls (methods with ResponseEntity<SomePayload> return types). I ended up writing a few POST handling methods which always result in redirects that were using a helper method ( return redirect("/location", statusCode) ) which built a ResponseEntity. Having been using ResponseEntity lately because I like the builder/explicit style, I just assumed that any redirection returned from a method in a controller would work with Flash scope.

So, having said all that, I don't have a special REST/stateful use case, so much as I was just expecting different behaviour.

Learned something new today - thanks for your quick feedback Rossen.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 20, 2017

Rossen Stoyanchev commented

Thanks for the extra details Edward Smith. Good to hear also about the preference to use ResponseEntity because of its API. Note that we are planning a similar API for view resolution purposes with #19775. That's for use on the reactive side, in the new WebFlux module but no reason why the same couldn't be applied to the WebMVC module as well.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 23, 2017

Edward Smith commented

Awesome, great to hear and thank you Rossen for the enhancement! VERY much looking forward to the Spring 5 GA.

@spring-projects-issues spring-projects-issues added type: enhancement in: web labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0 M5 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants