Updated Web State Machine Code Supports Back Button
Posted by David Chandler on November 28, 2005
I’ve updated the Web state machine example from my earlier CFDJ article. You can find the updated code here:
It now implements the POST-REDIRECT-GET (PRG) pattern and uses a page token to allow the back button to work better. I also updated the following files:
- sendEvent.cfm now appends pageToken to URL
- choose_customer.cfm removed form action attribute to show that sendEvent adds it
- choose_products.cfm removed form action attribute to show that sendEvent adds it
Refresh now works perfectly courtesy of PRG. The back button is trickier because when you click it, the browser actually loads its cached copy of the page, but what’s on the previous page no longer represents what’s stored in the user’s Session scope. So when you then click something on the page, you’re going forward again and will get to the right view state, but may see data from the future on your page. For example, in the sample app I’ve written, if you select two products, then use the back button, you will see a screen showing one product. But when you select another product, the page you get will show all THREE items you’ve now selected as far as the Session is concerned.
If you want to get really fancy, you can write code that keeps track of all the variables created in each state and “roll back” to your previous state by deleting any variables created forward from that point. Another possible solution is to use CFHTTPHEADER to set the no-cached directives. Then, when you click Back, the browser will be forced to make a trip to the server and the previous view will be shown with now current data.
If you get an error condition, you can display a hyperlink like “Resume what you were doing” with a link to flowctl.cfm. That will at least get the user back to the state that’s current on the server.