spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.76k stars 38.16k forks source link

WebFlux support for flash attributes [SPR-16026] #20575

Open spring-projects-issues opened 7 years ago

spring-projects-issues commented 7 years ago

Adrien Sanchez opened SPR-16026 and commented

Hello,

I faced an issue when wanting to perform a redirection with redirect attributes on a Spring Webflux application.

In Spring MVC, I used to add the RedirectAttributes parameter in my controller methods to configure the attributes/flash attributes that I wanted to transmit through my redirection.

However, in Spring Webflux, there seems to be no equivalent to the Spring MVC RedirectAttributes class. This is a problem as Spring Webflux controllers should be able to perform redirections with attributes too.

I tried using the redirectTo method of the reactive Rendering API, that works to perform a redirection, but there's no way to transmit parameters here too. Maybe this API could evolve to include this possibility ? And do you have any workaround that could be used at the moment?

Thanks and regards, Adrien


Affects: 5.0 RC3, 5.0 RC4, 5.0 GA

Reference URL: https://stackoverflow.com/questions/46474452/redirectattributes-not-working-with-spring-5

spring-projects-issues commented 7 years ago

Rossen Stoyanchev commented

No support for flash attributes currently in WebFlux.

You can add query params to the redirect URL /foo?q=1" and use URI vars to refer to model attributes (and URI vars from the current URL) "/foo?q={value}". WebFlux does not automatically append model attributes to the query and that reduces the need for something more explicit like RedirectAttributes.

spring-projects-issues commented 7 years ago

Adrien Sanchez commented

Hi Rossen, thanks for your answer.

If I understand correctly, we must go through query params to pass attributes in a redirection with Webflux? So after the redirection, those attributes will be visible in the browser URL, and we also need to map them explicitly in the code of the targeted controller to use them?

What I liked about RedirectAttributes was the fact that they were directly binded to the model in the targeted controller to be used, for instance, in a template (Thymeleaf in my case).

spring-projects-issues commented 7 years ago

Rossen Stoyanchev commented

Is it primarily about those attributes being in the model after the redirect? In other words are they mostly simple type attributes that can be appended to the query? In that case you could try a @ModelAttribute method to add query params to the model after a redirect using @RequestParam to access them. You can also inject all query params into the model attriute method with @RequestParam MultiValueMap<String,String> params. Also such a method can be shared with an @ControllerAdvice across all or a subset of controllers.

spring-projects-issues commented 7 years ago

Rossen Stoyanchev commented

In any case I am turning this into a feature request to support flash attributes.

spring-projects-issues commented 7 years ago

Adrien Sanchez commented

Thanks for your help, I'll try those workarounds. But yes, this feature would be very appreciated to perform redirections with model attributes easily.

spring-projects-issues commented 6 years ago

cafebaby commented

If you want to redirect the value passed, you can annotate @SessionAttributes({ "path", "user" }) on the class and use the Model model to store the value. Use @SessionAttribute("user") User user or Receive with Model in the receiving method

shiveenp commented 5 years ago

I have run into the same issue when trying to persist a user session info through redirects. Currently working around by storing the data in state as a nonce:{data} but would like to see this built in Webflux so we can do it properly.