Browser history management is an important aspect of a web application. An optimized history management will allow faster navigation and better responsiveness. The reason why GWT constitues a special case is its use of a single page (called RootPanel) approach ( SPA). The page can be seen as board on which components are added or deleted depending on the current context. Without history management in GWT, the application user will face the following issues: – If the user hits the back button, the browser will navigate to the previous page used by the user before entering the application and not to the previous state of the application. – If the user wants to bookmark the current state of the application, the bookmark will point to the first page of the application.
To tackle these issues, GWT introduced history management mechanisms starting from its 2.1 release. Activity and Place are helper classes that allow developpers to manage history by assigning names to application states using # ( Fragment Identifier). They work best in concordance with an MVP architecture. In this tutorial, we will go throught how to use Activities and Places to effectively manage history in a GWT application.
Requirements: Eclipse GWT plugin the application we built last tutorial: http://goo.gl/1m7HIL
Our application has two pages or states: a login and main page.
First of all, we are going to change our MVP setup. We are going to move our Presenter interface inside our view, and have it implemented by the activity which will play the role of the Presenter as well.
LoginView.java
Next we are going to create the LoginPlace and LoginActivity, which are required for managing history. A Place object holds the application state at a particular point.
LoginPlace.java
As you may notice, the LoginPlace class has an inner static class called Tokenizer which implements the PlaceTokenizer. This last is used for serializing the place name into a token that will be used by the browser to refer to the place. The next step is to define LoginActivity.java. Before doing so, we need to define our client factory which will be used for dependency injection.
ClientFactory.java
ClientFactoryImpl.java
We need add this piece of code in our module definition xml file.
Now, we are ready to define our activity :
LoginActivity.java
The next step is to define an ActivityMapper which maps each Place to an Activity. In addition, we are going to define a HistoryMapper that handles all the place tokens of our application.
MyActivityMapper.java
MyHistoryMapper.java
Finally, we need to update our entry point with all the new components: