Maintaining spring flash attribute after redirect

I have a controller that sets some flash attributes and causes a redirect. I also have an interceptor in front of the controller that will intercept the redirected URL and force another redirect. At this point, my Flash attributes are removed, as spring believes the redirect target is complete.

I would like to keep these attributes so that my second controller can access them after the second redirect.

Any possible way to achieve this?

Please, not that I cannot change the first controller that initially fills them, and I need these attributes to access the second redirection controller.

+4
source share
2 answers

Inside the HandlerInterceptor you should do the following

 FlashMap lastAttributes = RequestContextUtils.getInputFlashMap(request); // should hold the attributes from your last request FlashMap forNextRequest = RequestContextUtils.getOutputFlashMap(request); // will hold the attributes for your next request forNextRequest.putAll(lastAttributes); 
+4
source

I know there is already an accepted answer, but I would like to add some details to @Sotirios Delimanolis answer.

What am I doing:

 Map<String, ?> previousFlashAttributes = RequestContextUtils.getInputFlashMap(request); FlashMap flashAttributesForNextRequest = RequestContextUtils.getOutputFlashMap(request); if (previousFlashAttributes != null && flashAttributesForNextRequest != null) { flashAttributesForNextRequest.putAll(previousFlashAttributes); RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashAttributesForNextRequest, request, response); } 

The last line saves FlashMap in the output.

0
source

Source: https://habr.com/ru/post/1498021/


All Articles