Navigating in an Application

Plain Vaadin applications do not have normal web page navigation as they usually run on a single page, as all Ajax applications do. Quite commonly, however, applications have different views between which the user should be able to navigate. Also users often need to have direct links to specific views. The Navigator in Vaadin can be used for most cases of navigation. Views managed by the navigator automatically get a distinct URI, which can be used to be able to bookmark the views and their states and to go back and forward in the browser history.

Setting Up for Navigation

The Navigator class manages a collection of views that implement the View interface. The views can be either registered beforehand or acquired from a view provider. When registering, the views must have a name identifier and be added to a navigator with addView(). You can register new views at any point. Once registered, you can navigate to them with navigateTo().

Navigator manages navigation in a component container, which can be either a ComponentContainer (most layouts) or a SingleComponentContainer ( UI, Panel, or Window). The component container is managed through a ViewDisplay. Two view displays are defined: ComponentContainerViewDisplay and SingleComponentContainerViewDisplay, for the respective component container types. Normally, you can let the navigator create the view display internally, as we do in the example below, but you can also create it yourself to customize it.

Let us consider the following UI with two views: start and main. Here, we define their names with enums to be typesafe. We manage the navigation with the UI class itself, which is a SingleComponentContainer.

Java

  1. public class NavigatorUI extends UI {
  2. Navigator navigator;
  3. protected static final String MAINVIEW = "main";
  4. @Override
  5. protected void init(VaadinRequest request) {
  6. getPage().setTitle("Navigation Example");
  7. // Create a navigator to control the views
  8. navigator = new Navigator(this, this);
  9. // Create and register the views
  10. navigator.addView("", new StartView());
  11. navigator.addView(MAINVIEW, new MainView());
  12. }
  13. }

The Navigator automatically parses the URI to identify and show the View. The browser navigation back and forward are also handled by it.

Starting from 8.2.0 there is also PushState based navigation. This new way for the Navigator to manage URLs is described here as well. To enable this feature, add the PushStateNavigation annotation to your UI.

View Providers

You can create new views dynamically using a view provider that implements the ViewProvider interface. A provider is registered in Navigator with addProvider().

The ClassBasedViewProvider is a view provider that can dynamically create new instances of a specified view class based on the view name.

The StaticViewProvider returns an existing view instance based on the view name. The addView() in Navigator is actually just a shorthand for creating a static view provider for each registered view.

View Change Listeners

You can handle view changes also by implementing a ViewChangeListener and adding it to a Navigator. When a view change occurs, a listener receives a ViewChangeEvent object, which has references to the old and the activated view, the name of the activated view, as well as the parameters (the part of of URI after the viewname).

Implementing a View

Views can be any objects that implement the View interface. When the navigateTo() is called for the navigator, or the application is opened with the URI associated with the view, the navigator switches to the view and calls its enter() method.

To continue with the example, consider the following simple start view that just lets the user to navigate to the main view. It only pops up a notification when the user navigates to it and displays the navigation button.

Java

  1. /** A start view for navigating to the main view */
  2. public class StartView extends VerticalLayout implements View {
  3. public StartView() {
  4. setSizeFull();
  5. Button button = new Button("Go to Main View",
  6. new Button.ClickListener() {
  7. @Override
  8. public void buttonClick(ClickEvent event) {
  9. navigator.navigateTo(MAINVIEW);
  10. }
  11. });
  12. addComponent(button);
  13. setComponentAlignment(button, Alignment.MIDDLE_CENTER);
  14. }
  15. @Override
  16. public void enter(ViewChangeEvent event) {
  17. Notification.show("Welcome to the Animal Farm");
  18. }
  19. }

You can initialize the view content in the constructor, as was done in the example above, or in the enter() method. The advantage with the latter method is that the view is attached to the view container as well as to the UI at that time, which is not the case in the constructor.

Handling Path Parameters

By default the URLs managed through the Navigator have a URI fragment that contains the identifier of the View. The URI fragment is separated from the rest of the URL by a # character.

If the PushStateNavigation annotation is present on the UI the HTML5 History API is used. When using the PushState, the identifier is separated from the root path by a / like a real URL.

In addition to the View identifier, URI can contain additional parameters to be passed to views. The parameters are the part of the URI after the longest matching view identifier, separated by /. These parameters together with the identifier form the navigation state.

The navigation state can be used with Navigator in two ways: for navigating to a view and to a state within a view. The navigation state accepted by navigateTo() can have the view name at the root, followed by fragment parameters after a slash (“ /“). These parameters are passed to the enter() method in the View.

In the following example, we implement within-view navigation. Here we use the following declarative design for the view:

HTML

  1. <vaadin-vertical-layout size-full>
  2. <vaadin-horizontal-layout size-full :expand>
  3. <vaadin-panel caption="List of Equals" height-full width-auto>
  4. <vaadin-vertical-layout _id="menuContent" width-auto margin/>
  5. </vaadin-panel>
  6. <vaadin-panel _id="equalPanel" caption="An Equal" size-full :expand/>
  7. </vaadin-horizontal-layout>
  8. <vaadin-button _id="logout">Logout</vaadin-button>
  9. </vaadin-vertical-layout>

The view’s logic code would be as follows:

Java

  1. /** Main view with a menu (with declarative layout design) */
  2. @DesignRoot
  3. public class MainView extends VerticalLayout implements View {
  4. // Menu navigation button listener
  5. class ButtonListener implements Button.ClickListener {
  6. String menuitem;
  7. public ButtonListener(String menuitem) {
  8. this.menuitem = menuitem;
  9. }
  10. @Override
  11. public void buttonClick(ClickEvent event) {
  12. // Navigate to a specific state
  13. navigator.navigateTo(MAINVIEW + "/" + menuitem);
  14. }
  15. }
  16. VerticalLayout menuContent;
  17. Panel equalPanel;
  18. Button logout;
  19. public MainView() {
  20. Design.read(this);
  21. menuContent.addComponent(new Button("Pig",
  22. new ButtonListener("pig")));
  23. menuContent.addComponent(new Button("Cat",
  24. new ButtonListener("cat")));
  25. menuContent.addComponent(new Button("Dog",
  26. new ButtonListener("dog")));
  27. menuContent.addComponent(new Button("Reindeer",
  28. new ButtonListener("reindeer")));
  29. menuContent.addComponent(new Button("Penguin",
  30. new ButtonListener("penguin")));
  31. menuContent.addComponent(new Button("Sheep",
  32. new ButtonListener("sheep")));
  33. // Allow going back to the start
  34. logout.addClickListener(event ->
  35. navigator.navigateTo(""));
  36. }
  37. @DesignRoot
  38. class AnimalViewer extends VerticalLayout {
  39. Label watching;
  40. Embedded pic;
  41. Label back;
  42. public AnimalViewer(String animal) {
  43. Design.read(this);
  44. watching.setValue("You are currently watching a " +
  45. animal);
  46. pic.setSource(new ThemeResource(
  47. "img/" + animal + "-128px.png"));
  48. back.setValue("and " + animal +
  49. " is watching you back");
  50. }
  51. }
  52. @Override
  53. public void enter(ViewChangeEvent event) {
  54. if (event.getParameters() == null
  55. || event.getParameters().isEmpty()) {
  56. equalPanel.setContent(
  57. new Label("Nothing to see here, " +
  58. "just pass along."));
  59. return;
  60. } else
  61. equalPanel.setContent(new AnimalViewer(
  62. event.getParameters()));
  63. }
  64. }

The animal sub-view would have the following declarative design:

HTML

  1. <vaadin-vertical-layout size-full>
  2. <vaadin-label _id="watching" size-auto :middle :center/>
  3. <vaadin-embedded _id="pic" :middle :center :expand/>
  4. <vaadin-label _id="back" size-auto :middle :center/>
  5. </vaadin-vertical-layout>

The main view is shown in Navigator Main View. At this point, the URL would be http://localhost:8080/myapp/main/reindeer.

navigator mainview

Navigator Main View