8

What is the best way to make "Back" links, so that the application stays consistent after navigation.

  • onclick="history.go(-1)". Is this very harmful?
  • Using a stack in a backing bean that pops you the navigation case of the last view. You can do this by setting the navigation case with <f:setPropertyActionListener>.
  • None of these... other solutions.

Any help would be appreciated! Share your thoughts! Daniel

Daniel Szalay
  • 4,041
  • 12
  • 57
  • 103

2 Answers2

6

If you want to solve this by programmed navigation links, you can use a LinkedList as a stack. This way you can set boundaries for the number of stored navigation cases.

Example:

public class BackNavigationBean {

    public BackNavigationBean() {
        history = new LinkedList<String>();
    }
    private LinkedList<String> history;

    public LinkedList getHistory() {
        return history;
    }

    public void setLastPage(String navigationCase) {
        history.push(navigationCase);
        if (history.size() > 10) {
            history.pollLast();
        }
    }

    public String getLastPage() {
        return history.pop();
    }
}

So in 'forward' links:

<h:commandLink value="Forward" action="#{myBean.someMethod}">
    <f:setPropertyActionListener target="#{backNavigationBean.lastPage}"
                                 value="back_to_index" />
</h:commandLink>

And a 'back' link would be:

<h:commandLink value="Back"
               action="#{backNavigationBean.getLastPage}" />
Daniel Szalay
  • 4,041
  • 12
  • 57
  • 103
  • Hi. What's the scope of this Bean? Because if it's SessionScoped then it will be really difficult to manage the navigation due the stack will be pushed with links which will end up in false navigation. – LupoZ Apr 21 '17 at 13:39
5

I'm using:

  • MyFaces orchestra for providing conversation scope
  • <redirect /> for each navigation rule

This ensures that on pressing the "back" button or history.go(-1):

  • the data that has been available in the previous screen will be still there (because the conversation is still active)
  • the "resubmit" browser dialog won't open.

P.S. "backlink" has a different meaning

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140