Router Layouts and Nested Router Targets

RouterLayout Interface

All parent layouts of a navigation target component must implement the RouterLayout interface.

You can define a parent layout using the Route.layout() method.

Example: Render CompanyComponent inside MainLayout:

Java

  1. @Tag("div")
  2. @Route(value = "company", layout = MainLayout.class)
  3. public class CompanyComponent extends Component {
  4. }
Note
When using the @Route(“path”) annotation to define a route, the component by default renders in the <body> tag on the page. This is because the element returned by HasElement.getElement() is attached to the <body> tag.

Multiple Router Target Components

Where multiple router target components use the same parent layout, the parent layout instances remain the same when the user navigates between the child components.

See Updating Page Title on Navigation for more.

Multiple Parent Layouts

Use the @ParentLayout annotation to define a parent layout for components in the routing hierarchy.

You can create a parent layout for a parent layout, where necessary.

Example: MainLayout used for everything and MenuBar reused for views:

Java

  1. public class MainLayout extends Div
  2. implements RouterLayout {
  3. }
  4. @ParentLayout(MainLayout.class)
  5. public class MenuBar extends Div
  6. implements RouterLayout {
  7. public MenuBar() {
  8. addMenuElement(TutorialView.class, "Tutorial");
  9. addMenuElement(IconsView.class, "Icons");
  10. }
  11. private void addMenuElement(
  12. Class<? extends Component> navigationTarget,
  13. String name) {
  14. // implementation omitted
  15. }
  16. }
  17. @Route(value = "tutorial", layout = MenuBar.class)
  18. public class TutorialView extends Div {
  19. }
  20. @Route(value = "icons", layout = MenuBar.class)
  21. public class IconsView extends Div {
  22. }
  • MainLayout encapsulates MenuBar, which in turn encapsulates TutorialView or IconsView depending on where the user has navigated to.

ParentLayout Route Control

A parent layout can supplement the navigation route by adding to the route location.

This is done by annotating the parent layout with @RoutePrefix("prefix_to_add")

Example: PathComponent receives the some/path route.

Java

  1. @Route(value = "path", layout = SomeParent.class)
  2. public class PathComponent extends Div {
  3. // Implementation omitted
  4. }
  5. @RoutePrefix("some")
  6. public class SomeParent extends Div
  7. implements RouterLayout {
  8. // Implementation omitted
  9. }

Absolute Routes

You can use same parent component in many parts, without using a @RoutePrefix from the parent chain, or by only using it in defined parts.

This is done by adding absolute = true to either the @Route or @RoutePrefix annotations.

Example: Building a MyContent class to add “something” to multiple places in the SomeParent layout, without adding the route prefix to the navigation path:

Java

  1. @Route(value = "content", layout = SomeParent.class,
  2. absolute = true)
  3. public class MyContent extends Div {
  4. // Implementation omitted
  5. }
  • Even though the full path would typically be some/content, we actually get only content because it has been defined as absolute.

Example: Defining absolute in the middle of the chain.

Java

  1. @RoutePrefix(value = "framework", absolute = true)
  2. @ParentLayout(SomeParent.class)
  3. public class FrameworkSite extends Div
  4. implements RouterLayout {
  5. // Implementation omitted
  6. }
  7. @Route(value = "tutorial", layout = FrameworkSite.class)
  8. public class Tutorials extends Div {
  9. // Implementation omitted
  10. }
  • The bound route is framework/tutorial even though the full chain is some/framework/tutorial.

  • If a parent layout defines a @RoutePrefix, the “default” child could have its route defined as @Route("") and be mapped to the parent layout route. For example, in the case of Tutorials with route "" it would be mapped as framework/.