简体   繁体   中英

How do you rebuild the GWT History stack?

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?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM