Navigating Between Routes

You can use the RouterLink component to create links pointing to route targets in your application.

Navigation with RouterLink fetches the content of the new component without reloading the page. The page is updated in place.

Example: Using RouterLink for a simple navigation target.

Java

  1. void routerLink() {
  2. Div menu = new Div();
  3. menu.add(new RouterLink("Home", HomeView.class));
  4. }
  5. @Route(value = "")
  6. public class HomeView extends Component {
  7. }

Example: Using RouterLink for a navigation target with Route template route.

Java

  1. void routerLink() {
  2. Div menu = new Div();
  3. // user/123/edit
  4. menu.add(new RouterLink("Edit user details",
  5. UserProfileEdit.class, new RouteParameters("userID", "123")));
  6. // user/edit
  7. menu.add(new RouterLink("Edit my details",
  8. UserProfileEdit.class));
  9. }
  10. @Route("user/:userID?/edit")
  11. public class UserProfileEdit extends Div implements BeforeEnterObserver {
  12. private String userID;
  13. @Override
  14. public void beforeEnter(BeforeEnterEvent event) {
  15. userID = event.getRouteParameters().get("userID").
  16. orElse(CurrentUser.get().getUserID());
  17. }
  18. }
Note
Because the parameter is defined as optional, both URL links with and without the parameter are possible.

Example: Using RouterLink for navigation targets, with typed URL parameters, where the navigation target class implements HasUrlParameter.

Java

  1. void routerLink() {
  2. Div menu = new Div();
  3. menu.add(new RouterLink("Greeting",
  4. GreetingComponent.class, "default"));
  5. }
  6. @Route(value = "greet")
  7. public class GreetingComponent extends Div
  8. implements HasUrlParameter<String> {
  9. @Override
  10. public void setParameter(BeforeEvent event,
  11. String parameter) {
  12. setText(String.format("Hello, %s!", parameter));
  13. }
  14. }

It is also possible to navigate with standard <a href="company"> type links.

Standard links result in a page reload, but you can enable navigation without page reload by adding the router-link attribute, for example <a router-link href="company">Go to the company page</a>.

Server-Side Navigation

You can trigger navigation from the server side using UI.navigate(String), where the string parameter is the location to navigate to.

You can also use UI.navigate(Class<? extends Component> navigationTarget) or navigate(Class<? extends C> navigationTarget, RouteParameters parameters). This avoids having to generate the route string manually and triggers the browser location update and the addition of a new history state entry.

Example: Navigation to the company route target when clicking a button:

Java

  1. NativeButton button = new NativeButton(
  2. "Navigate to company");
  3. button.addClickListener(e ->
  4. button.getUI().ifPresent(ui ->
  5. ui.navigate("company"))
  6. );

Example: Navigation to the user/123/edit route target when clicking a button:

Java

  1. NativeButton editButton = new NativeButton(
  2. "Edit user details");
  3. editButton.addClickListener(e ->
  4. editButton.getUI().ifPresent(ui -> ui.navigate(
  5. UserProfileEdit.class,
  6. new RouteParameters("userID", "123")))
  7. );
Note
Router links work even if the session has expired. We recommend that you use them instead of handling navigation on the server side.