Skip to content

Flash Scope for Spring MVC (Without Spring Web Flow) [SPR-6464] #11130

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

Closed
spring-projects-issues opened this issue Nov 27, 2009 · 9 comments
Closed
Assignees
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Nov 27, 2009

Donny A. Wijaya opened SPR-6464 and commented

Geert Pante submitted the codes for this issue at: MOD-458.


Affects: 3.0 RC2

Reference URL: http://forum.springsource.org/showthread.php?t=73939&highlight=flash+scope

Attachments:

Issue Links:

31 votes, 29 watchers

@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

A couple of things to keep in mind when implementing flash scope:

  1. flash scope needs to be accessible outside of dispatcher servlet for things like spring security, since artifacts like AuthenticationFailureHandlers could need to store flash messages. Alternatively, spring security needs to be put under mvc's control.

  2. flash scope is traditionally stored in the session which exposes it to potential concurrency issues when multiple windows/tabs are used. See http://jira.codehaus.org/browse/GRAILS-1139 and http://markmail.org/message/wehwtbnwjtzwx4tm#query:+page:1+mid:loqkrwfk7mbwc6so+state:results for more information on this. Placing flash scope inside Window or Conversation scope could resolve this issue.

@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

Working filter-based prototype attached.

<!-- Allows attributes to be accessed on the next request -->
<filter>
    <filter-name>flashScope</filter-name>
    <filter-class>org.springframework.samples.petcare.util.FlashScopeFilter</filter-class>
</filter>
	
<filter-mapping>
    <filter-name>flashScope</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

@spring-projects-issues
Copy link
Collaborator Author

Micha Kiener commented

I'm going to add this for Spring 3.1 along the Conversation Management.
But I'd rather call it FlashMap instead of FlashScope as it is not a Scope implementation being exposed to the container.

By default, the map will be stored in window scope, if activated otherwise in session scope.

@spring-projects-issues
Copy link
Collaborator Author

Mike Youngstrom commented

A couple of questions:

  • Will this flash implementation storing mvc messages and retrieving them using the spring:messages tags?
  • Will it support storing ModelAttributes for passing a ModelAttribute across a redirect? Similar to @SessionAttribute but just for the redirect?

Thanks,
Mike

@spring-projects-issues
Copy link
Collaborator Author

Marcel Overdijk commented

Will this be part of Spring 3.1?

Als greenhouse project would benefit from this as it uses a 'custom' implementation.
I think a lot of people are interested in flash scope.

@spring-projects-issues
Copy link
Collaborator Author

Stephen Patrick commented

will this be part of 3.1? it will be very beneficial.

@spring-projects-issues
Copy link
Collaborator Author

Marcel Overdijk commented

@Stephen: I hope so. This issue is 8th on the list of top voted issues, so probably will get attention and implementation one day :-)

@spring-projects-issues
Copy link
Collaborator Author

Clemens Fuchslocher commented

The spring-mvc-showcase from Keith Donald contains an implementation of a custom flash scope in the package org.springframework.mvc.extensions.flash.

$ git clone 'https://github.com/SpringSource/spring-mvc-showcase.git'
$ cd spring-mvc-showcase
$ mvn tomcat:run
$ firefox http://localhost:8080/spring-mvc-showcase/

It is used in the FormController example to save a success message if JavaScript is disabled.

The FlashMapFilter adds all elements of the FlashMap to the request scope of the next request. The elements of the FlashMap are saved in the session.

There is one drawback with this implementation: One FlashScope per session could be a problem if the user works with multiple browser windows or tabs at the same time. For example, Stripes uses an additional parameter to distinguish between multiple flash scopes:

How FlashScope works

The FlashScope works by temporarily storing instances of itself in Session, and removing them on subsequent requests. As a result, when a FlashScope is used, an additional parameter is appended to redirect URLs to tell Stripes which FlashScope to use. Because of this parameter two or more browser windows (or tabs) in the same session will never get confused and access each others' FlashScopes.
...

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Flash attribute support is now available. The javadoc for FlashMap and FlashMapManager provides an overview of the underlying support for flash attributes in Spring MVC while RedirectAttributes describes how to add flash attributes in annotated controllers. If you have the latest source checked out, I recommend reviewing the updated reference documentation starting with "What's New In 3.1".

To answer some of the specific questions raised above:

Will this flash implementation storing mvc messages and retrieving them using the spring:messages tags?

Flash attributes are automatically merged into the model and are therefore available for rendering.

Will it support storing ModelAttributes for passing a ModelAttribute across a redirect? Similar to @SessionAttribute but just for the redirect?

@SessionAttribute is probably a better candidate for passing an @ModelAttribute across controller methods; however, you can also also use the RedirectAttributes argument add any attribute as a flash attribute and have it survive across a specific redirect.

One FlashScope per session could be a problem if the user works with multiple browser windows or tabs at the same time.

We've taken a slightly different approach. Rather than adding a framework-specific generated key, RedirectView "stamps" FlashMap instances with the path and query params of the target redirect URL, which are then checked after the redirect. This is somewhat similar but is based on information already present in the redirect URL. This approach will address most issues like polling from another tab or resource requests but won't eliminate the possibility of collisions entirely. Specifically there is no support for multiple windows. That said if flash attributes are used in redirect scenarios only such collisions are much less likely. Furthermore, given that we already filter based on the query params of redirect URLs, it would be a small step to add a generated query param to the redirect URL if required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants