Search code examples
javaspring-webflow

Invalidate previous page snapshot in Spring Webflow


I am using Spring Webflow and I am redirecting from a view-state to action-state to view-state. Now, when the user is on second view-state, I don't want the user to be able to click the browser back button and go to the previous page.

I found that, history="invalidate" can be used to invalidate the snapshot of previous page. I tried using it in the transition. However, its not working (the browser back button is not disabled).

So, what can I add to my webflow states in order to disable the back button and only allow the user to use the navigation controls provided inside the page?

Thank you


Solution

  • To which state did you add the history attribute?

    First, <action-state> doesn't support it (https://jira.spring.io/browse/SWF-1481).

    You'd have to add it to the first <view-state>'s transition. Which, if you only wanted to do it conditionally on something that happened in the <action-state>, wouldn't be sufficient. We ended up creating Java code to call from our <action-state> method to do this.

    /**
     * Invalidate WebFlow history. For use in action-state, where the transition element is not able
     * to specify to invalidate history.
     */
    public static void invalidateHistory() {
        RequestContext context = RequestContextHolder.getRequestContext();
    
        synchronized(context.getFlowExecutionContext()) {
            DefaultFlowExecutionRepository r =
                    (DefaultFlowExecutionRepository)
                    ((FlowExecutorImpl)
                        context.getActiveFlow().
                        getApplicationContext().
                        getBean(FlowExecutor.class)).getExecutionRepository();
            r.removeAllFlowExecutionSnapshots((FlowExecution)context.getFlowExecutionContext());
        }
    }
    

    (Also, note that "invalidate" invalidates the state and all before it. If you only want to prevent that single state, you'd use "discard" instead. https://docs.spring.io/spring-webflow/docs/current/reference/html/views.html#view-backtracking)