Hello.
I am trying to extend the NavigationHandler in JSF 1.2, and to keep a stack of the visited pages (more precisely, the view-ids, along with the action and the output).
I try to implement a custom action, such as "go_back", that will take me back to the previous page.
My current NavigationHandler:
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import java.util.Stack;
public class NavigationManager extends NavigationHandler {
NavigationHandler _base;
private Stack trailPointStack;
public NavigationManager(NavigationHandler base) {
super();
_base = base;
trailPointStack = new Stack();
}
@Override
public void handleNavigation(FacesContext fc, String actionMethod, String actionName) {
NavigationPoint point;
String currentAction = actionName;
String currentMethod = actionMethod;
if (actionName.equals("go_back") && (trailPointStack.size() > 0)) {
point = (NavigationPoint) trailPointStack.pop();//better check if there is something in there
//currentAction = null;
//currentMethod = null;
currentAction = point.getAction();
currentMethod = point.getActionMethod();
fc.getViewRoot().setViewId(point.getViewId());
} else {
point = new NavigationPoint(actionName, actionMethod, fc.getViewRoot().getViewId());
trailPointStack.push(point);
}
//check stack size to be less than 6 items
while (trailPointStack.size() > 5) {
trailPointStack.removeElementAt(0);
}
_base.handleNavigation(fc, currentMethod, currentAction);
}
}
The NavigationPoint is just a simple class with 3 Strings, for the actionName, actionMethod and ViewId.
My navigation rules, in faces-config.xml:
<navigation-rule>
<description>Index to subpages</description>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>i_to_1</from-outcome>
<to-view-id>/page_a1.jsp</to-view-id>
<redirect />
</navigation-case>
<navigation-case>
<from-outcome>to_page_a2</from-outcome>
<to-view-id>/page_a2.jsp</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<navigation-rule>
<description>From page a1</description>
<from-view-id>/page_a1.jsp</from-view-id>
<navigation-case>
<from-outcome>to_page_a2</from-outcome>
<to-view-id>/page_a2.jsp</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<navigation-rule>
<description>From page a2</description>
<from-view-id>/page_a2.jsp</from-view-id>
<navigation-case>
<from-outcome>to_page_a1</from-outcome>
<to-view-id>/page_a1.jsp</to-view-id>
<redirect />
</navigation-case>
<navigation-case>
<from-outcome>to_index</from-outcome>
<to-view-id>/index.jsp</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
I only have 3 pages, index.jsp, page_a1.jsp and page_a2.jsp.
You can see in the navigation cases, the connections between them. What I want is to be able to "go_back" from page_a2.jsp, to either page_a1.jsp, or to index.jsp.
The normal navigation works fine: Index -> P1 -> P2 -> P1 -> P2 -> Index; no problem.
If I do: Index -> P1 -> P2
I will have on the stack:
bottom
1:index.jsp/i_to_1 -> page_a1.jsp
2:page_a1.jsp/to_page_a2 -> page_a2.jsp
top
When I try "go_back" from P2, I expect it to go back to page 1. It doesn't (the page is just reloaded). If I try it a second time, it works.
I think it's because on the first try, I pop from the stack, and it tries with the action "to_page_a2" - fails. The second time, it pops again from the stack, but now it tries with "i_to_1", and this.. somehow, works.
Can anyone help me with this? I hope my explanations were clear enough - if not, please ask.
Any similar idea is also welcome. I should mention that it was 2 days ago that I started using JSF, and it's not very clear to me all that happens there.
Thank you, Alex