GWT History Mechanism
GWT History mechanism is similar to the Ajax history implementations such as RSH (Really Simple History). Basic idea is to track application internal state in the URL fragment identifier. Main advantages of this mechanism are:
- It provides browser history reliable.
- It provides good feedback to the user.
- It is bookmarkable i.e., the user can create a bookmark to the current state and save it or can email it etc.
GWT History Syntax
GWT History Tokens
A token is simply a string that the application can parse to return to a particular state. This token will be saved in browser history as a URL fragment (in the location bar, after the “#”), and this fragment is passed back to the application when the user goes back or forward in history, or follows a link.
Example: History token name tutoraspire.
http://www.example.com/com.example.gwt.HistoryExample/HistoryExample.html#tutoraspire
GWT History Common Methods
Modifier and Types | Method | Description |
---|---|---|
static HandlerRegistration | addValueChangeHandler (ValueChangeHandler<java.lang.String> handler) | It adds a ValueChangeEvent handler to be informed of changes to the browser’s history stack. |
static void | back() | It is a programmatic equivalent to the user pressing the browser’s ‘back’ button. |
static java.lang.String | encodeHistoryToken(java.lang.String historyToken) | It encodes a history token for use as part of a URI. |
static void | fireCurrentHistoryState() | It calls a ValueChangeHandler.onValueChange (com.google.gwt.event.logical.shared.ValueChangeEvent) events with the current history state. |
static void | forward() | It is a programmatic equivalent to the user pressing the browser’s ‘forward’ button. |
static java.lang.String | getToken() | It gets the current history token. |
static void | newItem(java.lang.String historyToken) | It adds a new browser history entry. |
static void | newItem(java.lang.String historyToken, boolean issueEvent) | It adds a new browser history entry. |
static void | replaceItem(java.lang.String historyToken) | It replaces the current history token on top of the browsers history stack. |
static void | replaceItem(java.lang.String historyToken, boolean issueEvent) | It replaces the current history token on top of the browsers history stack. |
GWT History Example
Output:
GWT Hyperlink Widgets
Hyperlinks are convenient to use to incorporate history support into an application. Hyperlink widgets are GWT widgets that look like regular HTML anchors. You can associate a history token with the Hyperlink, and when it is clicked, the history token is automatically added to the browser history stack. The History.newItem(token) step is done automatically.
Handling an onValueChange() callback
The first step of handling the onValueChange() callback method in a ValueChangeHandler is to get the new history token with ValueChangeEvent.getValue() then we will parse the token. Once the token is parsed, we can reset the state of the application.
When the onValueChange() method is invoked, application handles two cases:
- The application was just started and was passed a history token.
- The application is already running and was passed a history token.