You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-navigator.asciidoc 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ---
  2. title: Navigating in an Application
  3. order: 9
  4. layout: page
  5. ---
  6. [[advanced.navigator]]
  7. = Navigating in an Application
  8. Plain Vaadin applications do not have normal web page navigation as they usually
  9. run on a single page, as all Ajax applications do. Quite commonly, however,
  10. applications have different views between which the user should be able to
  11. navigate. Also users often need to have direct links to specific views. The [classname]#Navigator# in Vaadin can be used for most cases of
  12. navigation. Views managed by the navigator automatically get a distinct URI,
  13. which can be used to be able to bookmark the views and their states
  14. and to go back and forward in the browser history.
  15. [[advanced.navigator.navigating]]
  16. == Setting Up for Navigation
  17. The [classname]#Navigator# class manages a collection of __views__ that
  18. implement the [interfacename]#View# interface. The views can be either
  19. registered beforehand or acquired from a __view provider__. When registering,
  20. the views must have a name identifier and be added to a navigator with
  21. [methodname]#addView()#. You can register new views at any point. Once
  22. registered, you can navigate to them with [methodname]#navigateTo()#.
  23. [classname]#Navigator# manages navigation in a component container, which can be
  24. either a [interfacename]#ComponentContainer# (most layouts) or a
  25. [interfacename]#SingleComponentContainer# ( [classname]#UI#, [classname]#Panel#,
  26. or [classname]#Window#). The component container is managed through a
  27. [interfacename]#ViewDisplay#. Two view displays are defined:
  28. [classname]#ComponentContainerViewDisplay# and
  29. [classname]#SingleComponentContainerViewDisplay#, for the respective component
  30. container types. Normally, you can let the navigator create the view display
  31. internally, as we do in the example below, but you can also create it yourself
  32. to customize it.
  33. Let us consider the following UI with two views: start and main. Here, we define
  34. their names with enums to be typesafe. We manage the navigation with the UI
  35. class itself, which is a [interfacename]#SingleComponentContainer#.
  36. [source, java]
  37. ----
  38. public class NavigatorUI extends UI {
  39. Navigator navigator;
  40. protected static final String MAINVIEW = "main";
  41. @Override
  42. protected void init(VaadinRequest request) {
  43. getPage().setTitle("Navigation Example");
  44. // Create a navigator to control the views
  45. navigator = new Navigator(this, this);
  46. // Create and register the views
  47. navigator.addView("", new StartView());
  48. navigator.addView(MAINVIEW, new MainView());
  49. }
  50. }
  51. ----
  52. The [classname]#Navigator# automatically parses the URI to identify and show the [interfacename]#View#. The browser navigation back and forward are also handled by it.
  53. Starting from [literal]#++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 [classname]#PushStateNavigation# annotation to your UI.
  54. [[advanced.navigator.navigating.viewprovider]]
  55. === View Providers
  56. You can create new views dynamically using a __view provider__ that implements
  57. the [interfacename]#ViewProvider# interface. A provider is registered in
  58. [classname]#Navigator# with [methodname]#addProvider()#.
  59. The [methodname]#ClassBasedViewProvider# is a view provider that can dynamically
  60. create new instances of a specified view class based on the view name.
  61. The [methodname]#StaticViewProvider# returns an existing view instance based on
  62. the view name. The [methodname]#addView()# in [classname]#Navigator# is actually
  63. just a shorthand for creating a static view provider for each registered view.
  64. [[advanced.navigator.navigating.viewchangelistener]]
  65. === View Change Listeners
  66. You can handle view changes also by implementing a
  67. [interfacename]#ViewChangeListener# and adding it to a [classname]#Navigator#.
  68. When a view change occurs, a listener receives a [classname]#ViewChangeEvent#
  69. object, which has references to the old and the activated view, the name of the
  70. activated view, as well as the parameters (the part of of URI after the viewname).
  71. [[advanced.navigator.view]]
  72. == Implementing a View
  73. Views can be any objects that implement the [interfacename]#View# interface.
  74. When the [methodname]#navigateTo()# is called for the navigator, or the
  75. application is opened with the URI associated with the view, the
  76. navigator switches to the view and calls its [methodname]#enter()# method.
  77. To continue with the example, consider the following simple start view that just
  78. lets the user to navigate to the main view. It only pops up a notification when
  79. the user navigates to it and displays the navigation button.
  80. [source, java]
  81. ----
  82. /** A start view for navigating to the main view */
  83. public class StartView extends VerticalLayout implements View {
  84. public StartView() {
  85. setSizeFull();
  86. Button button = new Button("Go to Main View",
  87. new Button.ClickListener() {
  88. @Override
  89. public void buttonClick(ClickEvent event) {
  90. navigator.navigateTo(MAINVIEW);
  91. }
  92. });
  93. addComponent(button);
  94. setComponentAlignment(button, Alignment.MIDDLE_CENTER);
  95. }
  96. @Override
  97. public void enter(ViewChangeEvent event) {
  98. Notification.show("Welcome to the Animal Farm");
  99. }
  100. }
  101. ----
  102. You can initialize the view content in the constructor, as was done in the
  103. example above, or in the [methodname]#enter()# method. The advantage with the
  104. latter method is that the view is attached to the view container as well as to
  105. the UI at that time, which is not the case in the constructor.
  106. [[advanced.navigator.pathparam]]
  107. == Handling Path Parameters
  108. By default the URLs managed through the [classname]#Navigator# have a URI fragment
  109. that contains the identifier of the [interfacename]#View#. The URI fragment is
  110. separated from the rest of the URL by a [literal]#++#++# character.
  111. If the [classname]#PushStateNavigation# annotation is present on the [classname]#UI#
  112. the HTML5 History API is used. When using the PushState, the identifier is separated
  113. from the root path by a [literal]#++/++# like a real URL.
  114. In addition to the View identifier, URI can contain additional parameters to be
  115. passed to views. The parameters are the part of the URI after the longest matching view identifier, separated by [literal]#++/++#. These parameters together with the identifier
  116. form the __navigation state__.
  117. The navigation state can be used with [classname]#Navigator# in two ways: for
  118. navigating to a view and to a state within a view. The navigation state accepted by
  119. [methodname]#navigateTo()# can have the view name at the root, followed by
  120. fragment parameters after a slash (" [literal]#++/++#"). These parameters are
  121. passed to the [methodname]#enter()# method in the [interfacename]#View#.
  122. In the following example, we implement within-view navigation. Here we use the
  123. following declarative design for the view:
  124. [source, html]
  125. ----
  126. <vaadin-vertical-layout size-full>
  127. <vaadin-horizontal-layout size-full :expand>
  128. <vaadin-panel caption="List of Equals" height-full width-auto>
  129. <vaadin-vertical-layout _id="menuContent" width-auto margin/>
  130. </vaadin-panel>
  131. <vaadin-panel _id="equalPanel" caption="An Equal" size-full :expand/>
  132. </vaadin-horizontal-layout>
  133. <vaadin-button _id="logout">Logout</vaadin-button>
  134. </vaadin-vertical-layout>
  135. ----
  136. The view's logic code would be as follows:
  137. [source, java]
  138. ----
  139. /** Main view with a menu (with declarative layout design) */
  140. @DesignRoot
  141. public class MainView extends VerticalLayout implements View {
  142. // Menu navigation button listener
  143. class ButtonListener implements Button.ClickListener {
  144. String menuitem;
  145. public ButtonListener(String menuitem) {
  146. this.menuitem = menuitem;
  147. }
  148. @Override
  149. public void buttonClick(ClickEvent event) {
  150. // Navigate to a specific state
  151. navigator.navigateTo(MAINVIEW + "/" + menuitem);
  152. }
  153. }
  154. VerticalLayout menuContent;
  155. Panel equalPanel;
  156. Button logout;
  157. public MainView() {
  158. Design.read(this);
  159. menuContent.addComponent(new Button("Pig",
  160. new ButtonListener("pig")));
  161. menuContent.addComponent(new Button("Cat",
  162. new ButtonListener("cat")));
  163. menuContent.addComponent(new Button("Dog",
  164. new ButtonListener("dog")));
  165. menuContent.addComponent(new Button("Reindeer",
  166. new ButtonListener("reindeer")));
  167. menuContent.addComponent(new Button("Penguin",
  168. new ButtonListener("penguin")));
  169. menuContent.addComponent(new Button("Sheep",
  170. new ButtonListener("sheep")));
  171. // Allow going back to the start
  172. logout.addClickListener(event ->
  173. navigator.navigateTo(""));
  174. }
  175. @DesignRoot
  176. class AnimalViewer extends VerticalLayout {
  177. Label watching;
  178. Embedded pic;
  179. Label back;
  180. public AnimalViewer(String animal) {
  181. Design.read(this);
  182. watching.setValue("You are currently watching a " +
  183. animal);
  184. pic.setSource(new ThemeResource(
  185. "img/" + animal + "-128px.png"));
  186. back.setValue("and " + animal +
  187. " is watching you back");
  188. }
  189. }
  190. @Override
  191. public void enter(ViewChangeEvent event) {
  192. if (event.getParameters() == null
  193. || event.getParameters().isEmpty()) {
  194. equalPanel.setContent(
  195. new Label("Nothing to see here, " +
  196. "just pass along."));
  197. return;
  198. } else
  199. equalPanel.setContent(new AnimalViewer(
  200. event.getParameters()));
  201. }
  202. }
  203. ----
  204. The animal sub-view would have the following declarative design:
  205. [source, html]
  206. ----
  207. <vaadin-vertical-layout size-full>
  208. <vaadin-label _id="watching" size-auto :middle :center/>
  209. <vaadin-embedded _id="pic" :middle :center :expand/>
  210. <vaadin-label _id="back" size-auto :middle :center/>
  211. </vaadin-vertical-layout>
  212. ----
  213. The main view is shown in <<figure.advanced.navigator.mainview>>. At this point,
  214. the URL would be [literal]#++http://localhost:8080/myapp/main/reindeer++#.
  215. [[figure.advanced.navigator.mainview]]
  216. .Navigator Main View
  217. image::img/navigator-mainview.png[]