tags:

views:

862

answers:

2

I have a larger application that I'm working with but the GWT History documentation has a simple example that demonstrates the problem. The example is copied for convenience:

public class HistoryTest implements EntryPoint, ValueChangeHandler
{
    private Label lbl = new Label();

    public void onModuleLoad()
    {
        Hyperlink link0 = new Hyperlink("link to foo", "foo");
        Hyperlink link1 = new Hyperlink("link to bar", "bar");
        Hyperlink link2 = new Hyperlink("link to baz", "baz");

        String initToken = History.getToken();

        if (initToken.length() == 0)
        {
            History.newItem("baz");
        }

        // Add widgets to the root panel.
        VerticalPanel panel = new VerticalPanel();
        panel.add(lbl);
        panel.add(link0);
        panel.add(link1);
        panel.add(link2);
        RootPanel.get().add(panel);

        History.addValueChangeHandler(this);        // Add history listener
        History.fireCurrentHistoryState();
    }

    @Override
    public void onValueChange(ValueChangeEvent event)
    {
        lbl.setText("The current history token is: " + event.getValue());
    }
}

The problem is that if you refresh the application, the history stack gets blown away. How do you preserve the history so that if the user refreshes the page, the back button is still useful?

+1  A: 

I have just tested it with Firefox and Chrome for my application and page refresh does not clear the history. Which browser do you use? Do you have the

<iframe src="javascript:''" id='__gwt_historyFrame' style='position:absolute;width:0;height:0;border:0'></iframe>

in your HTML?

kaboom
Yes, the history frame was in place. It turns out this is not a technical issue under my control and I feel rather silly now for asking.A few things were at play. First, the GWT browser loses the history on refresh (sometimes I forget to compile). Second, IE acts strangely with regards to the history. Third, a few developers weren't properly pushing history tokens onto the stack.Thanks for taking a look at this for me, sorry for the trouble.
Michael
A: 

GWT has catered for this problem by providing the History object. By making a call to it's static method History.newItem("your token"), you will be able to pass a token into your query string.

However you need to be aware that any time there is a history change in a gwt application, the onValueChange(ValueChangeEvent event){} event is fired, and in the method you can call the appropriate pages. Below is a list of steps which i use to solve this problem.

  1. Add a click listener to the object that needs too call a new page. In handling the event add a token to the history.(History.newItem("new_token").

  2. Implement the ValueChangeHandler in the class that implements your EntryPoint.

  3. Add onValueChangeHandler(this) listener to the class that implements the EntryPoint. Ensure that the line is add in the onModuleLoad() method (it is important it is added in this method) of the class that implements the EntryPoint(pretty obvious ha!)

  4. Finally implement onValueChange(ValueChangeEvent event){ //call a new page } method.

That's it

lord kinful