I'm new to Spring Web Flow (2.3.1) and this is what I'm trying to do:
I have a JSP page which lists a paginated table of Books at the bottom of the page for the Author selected at the top of the page. I have a button/link at the top 'Add Book' (just below the Author drop-down) clicking which launches a Spring web flow that takes to a page where user can enter details of the Book in steps 1, 2 & 3 (in 3 different views/pages). Clicking Save creates the new Book and should take the user back to the view with paginated list of books.
Now, I want to add a flash attribute (success message) after the Save action and take user back to Books page (paginated), have the 'previous' author pre-selected and show the success message.
I have the following the web flow XML for the end state:
<end-state id="home" view="externalRedirect:/books/" >
<output name="author" value="book.author" />
</end-state>
The reason I'm doing the externalRedirect is that I want the URL to read as if the user just clicked on the Books listing page after adding a new Book. If I don't do the redirect but instead point to the view name from tiles.xml I see the flash message correctly but the URL still shows the web flow e.g., ?execution=e1s1
. In both cases the author is not automatically selected.
How do I preserve the flash success message AND the Author selection after a redirect?
Does output
variable have any meaning in an external redirect?
I'm also setting the following in the Save action:
requestContext.getFlowScope().put("authorId", book.getAuthorId());
the 'output' tag is meant to be used as a container to transfer pojos between subflows flows and the parent flow callers. However, the following enhancement request:
https://jira.spring.io/browse/SWF-1561
expanded the role of the 'output' tag to allow the transfer of flash variables from the 'end-state' of a webflow to a spring MVC controller's flashMap.
Unfortunately, this enhancement did NOT include what you want to achieve which is passing variables via an externalRedirect from flow A -> new Flow A. So as with any 3rd party lib that doesn't have the desired functionality... we're going to have to 'hack' it.
1. First you need to have your FlowHandlerAdapter configured like this to utilize the enhancement above: the "saveOutputToFlashScopeOnRedirect" set to true is the important part for this discussion (set to false by default).
2. You will need to create (extend) and configure a FlowExecutionListenerAdapter.
And init the bean like this:
Note: Change "com.foo.bar.flowexeclisteners" to your actual package path.
3. The FlowExecutionListenerAdapter above allows to to monitor life cycle events of Spring Webflow framework by @Overriding certain methods. In our case, I chose to @Override the sessionCreating() method but you can use from many different methods to meet your use case or to increase efficiency.
http://docs.spring.io/spring-webflow/docs/current/api/org/springframework/webflow/execution/FlowExecutionListenerAdapter.html
So given the above configuration we can now begin passing back variables in 2 ways. One via sessionMap or Two via the 'output' tag which is now configured to be stored in the flashMap (of Spring MVC not flashScope). Here is an example 'end-state' demonstrating this.
4. When this 'end-state' is triggered (if you place a break point inside our FlowExecutionListenerAdapter) you will see that flashMapMvc will hold your 'responseMsg' variable and that 'author' variable will be in the sessionMap. The static method 'putAllFlashMapToFlashScope' will automatically make your 'responseMsg' avaliable for consumption by your view but for the sessionMap you will need to explicitly extract your variable out in your receiving flow like so:
A few notes here:
The hacky part of all this is we have to convert the flashMapMvc to flashScopeWebFlow because they are not compatible (2 different containers). I used a static method 'putAllFlashMapToFlashScope()' within the FlowExecutionListenerAdapater class to demonstrate this. I don't think this is a good practice but I just did it here for brevity so you can see what exactly what the issues are and how to solve them.
sessionMap variables will stay during the entire session and across all flows. If you use this map becareful of this point.
I included other (unused) maps in the FlowExecutionListenerAdapter to demonstrate what you have access to.
Obviously this is a hack solution and what really needs to happen is an enhancement request to achieve what your trying to do (flow A ends) -> pass output back to -> (new flow A) via external redirect.
Because of issues like this I've personally stopped using WebFlow for all use cases and have limited it only to simple cases (i.e page A -> Page B -> page C) and now I use Spring MVC exclusively for all other use cases.
Sorry for the long answer but this is not a trivial issue :)