Search code examples
springspring-webflowspring-webflow-2

Spring Webflow prevent user from going back to the start of the flow after flow completes


Spring Webflow prevent user from going back to the start of the flow after flow completes and the user is taken to an end state. Clicking the browser back button should take him to an error page and not to the start of the flow. Let me know if i can provide any further info.


Solution

  • When reentering a flow after a user clicks the back button you need some type of condition check or to trigger an exception where either can be caught and handled to redirect to desired page.

    Possible Solutions:

    1. Add to the user's session a variable that indicates a particular flow can no longer be entered by the user and check for this variable at the start of the flow.

          <decision-state id="checkBackButtonClick">
            <if test="externalContext.sessionMap.preventBackButtonClick" then="redirectToHomePage" else="continueToEditState"/>
          </decision-state> 
    
    ....
    
          <end-state id="end" view="externalRedirect:/pojos/#{somePojo.id}">
               <on-entry>
                    <set name="externalContext.sessionMap.preventBackButtonClick" value="true" type="java.lang.Boolean"/>
    
                </on-entry>
          </end-state>
    

    Although, the solution above appears to be a "hack" I think it is a good solution if you only need this behavior for a 1 or a few flows and it does not require much to implement... (no extending classes, overriding methods, etc...)

    2. Another possible solution is to place a 'require=true' attribute/value on a input parameter for a flow.

    <input name="id" required="true"/>
    

    So if the user clicks back button without the required param in the url the flow will trigger an exception

    org.springframework.webflow.engine.FlowInputMappingException:
    

    And depending on how you handle exceptions in your webflows... you can catch this exception and redirect to your desired page/flow. Assumes you have 'input' tag params.

    3. Implement your flow by extending AbstractFlowHandler in java which will give you a tremendous amount of customization power. Obviously 3rd option comes with more coding/work. Use it as a last resort

    http://www.springbyexample.org/examples/simple-spring-web-flow-webapp-code-example.html

    ftp://courses.rvrjcce.ac.in/Java%20Sessions/Software/spring-webflow-2.1.1.RELEASE/docs/spring-webflow-reference/html/ch11s04.html

    http://www.jonathanhui.com/spring-web-flow-flow-programming