I have a following question. We have an application that is a basically a set of JSPs and action classes that control the interaction with the user. The actions set up some parameters required for the page to display properly. The urls in the system always contain the name of the event which defines which page we should go next.
Now, almost every page has a Cancel button that should lead to the previous page. From the start each cancel button url was strictly defined, but eventually, as the system grew larger and larger, it turned out to be quite hard to program the logic for this button so that it would lead exactly to the previous page.
For example, suppose we have three pages, A, B and C. Page A has a link leading to page B, and B has a link leading to C. Therefore C has an url with even leading to B (and also it contains the key of the entity to display on B). However, suppose page A was modified and now also has a link on C. Now we need to check on C page from there we came and set the URL appropriately, and the logic becomes pretty mixed.
So, I proposed to my team the following solution. The user session should contain a special object called the CancelStack. Every action, that leads to the page, should push it’s url inside the stack (containing the event and some additional data that is required). On every page the cancel button should now have an url leading to a special event, called cancelStack.
What the cancelStack action does is this:
- Retrieve the cancel stack from the session.
- Pop the last url and DO NOT use it.
- Pop the url again and redirect to that URL.
Why do we retrieve the last url without using? Suppose we have pages A and B, A leading to B. Action for A places it’s url inside the stack, and this should be the cancel url for the B page. Now, action for B places it’s url inside the stack. Therefore I pop it without using, and then pop the first url, redirect to A action, and this action adds the A url again to the stack (therefore stack size decreases only by 1, not by 2).
It seems like a pretty good scheme, however it seems rather strange that the top element of stack is popped without using. Therefore I have a question. Is there any design patter in order to store the sequence of URLs inside the session in order to organise cancel buttons properly?
What you did seems reasonable to me. To be honest I can’t think of a design pattern addressing your issue right now, but I think if besides the
cancelStackyou keep a reference to thecurrentURLso that you push thecurrentURLto the stack only if you don’t end up in a cancel page you will get rid of the extrapopthat annoys you.Otherwise you just
popthe top ofcancelStack.E.g. in your example:
currentUrlis A. Action for A is not cancel socurrentUrli.e.Ais pushed to thecancelStack. Then it is action forBandcurrentURLisBbut action is cancel soBis not placed in the stack. So the top ofcancelStackisA(whilecurrentUrlisB). So if youpopthecancelStackyou retrieveA(no extrapopneeded)