gwt-presenter Hyperlink gotcha: URL in address bar getting wiped out
Posted by David Chandler on October 8, 2009
Just a quick note in the Headsmack department related to navigation between views with gwt-presenter: if you attach a ClickHandler that fires a PlaceRequestEvent to a Hyperlink, but forget to supply the history token, the browser will correctly navigate to the requested Place; however, the placeId and any parameters on the URL will immediately be wiped out in the browser address bar. The reason is that after the ClickHandler fires, GWT replaces the URL in the address bar with the history token from the Hyperlink, which, in the example below, is empty because it’s never set.
// Don't do this Hyperlink hyperlink = new Hyperlink(); hyperlink.setText("click here"); hyperlink.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { eventBus.fireEvent(new PlaceRequestEvent(placeRequest)); } });
This is a case of making it too hard (or more likely, accidentally leaving the ClickHandler in place when you decide to try a Hyperlink instead of a Button). Thanks to the gwt-presenter PlaceManager, which listens for History ValueChangeEvents, all you need is this:
Hyperlink hyperlink = new Hyperlink("click here", placeRequest.toString());
That’s so clean it makes me want to use Hyperlinks everywhere.
See also a previous post on this topic.
Leave a Reply