Class Router

java.lang.Object
com.vaadin.flow.router.Router
All Implemented Interfaces:
Serializable

public class Router extends Object implements Serializable
The router takes care of serving content when the user navigates within a site or an application.
Since:
1.0.
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • Router

      public Router(RouteRegistry registry)
      Constructs a new router with the given route registry and a DefaultRouteResolver.
      Parameters:
      registry - the route registry to use, not null
  • Method Details

    • initializeUI

      public void initializeUI(UI ui, Location location)
      Enables navigation for a new UI instance. This initializes the UI content based on the location used for loading the UI and sets up the UI to be updated when the user navigates to some other location.
      Parameters:
      ui - the UI that navigation should be set up for
      location - the location object of the route
      Since:
      3.0
    • resolveNavigationTarget

      public Optional<NavigationState> resolveNavigationTarget(String pathInfo, Map<String,String[]> parameterMap)
      Resolve the navigation target for given path and parameter map using the router routeResolver.
      Parameters:
      pathInfo - the path relative to the application
      parameterMap - A mapping of parameter names to arrays of parameter values
      Returns:
      NavigationTarget for the given path and parameter map if found
    • resolveNavigationTarget

      public Optional<NavigationState> resolveNavigationTarget(Location location)
      Resolve the navigation target for given Location using the router routeResolver.
      Parameters:
      location - the location object of the route
      Returns:
      NavigationTarget for the given location if found
      Since:
      3.0
    • resolveRouteNotFoundNavigationTarget

      public Optional<NavigationState> resolveRouteNotFoundNavigationTarget()
      Resolve a navigation target with an empty NotFoundException.
      Returns:
      an instance of NavigationState for NotFoundException or empty if there is none in the application.
      Since:
      3.0
    • resolvePageTitle

      public Optional<String> resolvePageTitle(Class<? extends Component> navigationTarget, RouteParameters routeParameters)
      Resolves the page title of the given navigation target without creating an instance of it, using empty query parameters.
      Parameters:
      navigationTarget - the navigation target to resolve the title for, not null
      routeParameters - the route parameters the target is resolved with, not null
      Returns:
      the resolved title, or an empty Optional if the target declares no title and no default generator is available
      Since:
      25.2
      See Also:
    • resolvePageTitle

      public Optional<String> resolvePageTitle(Class<? extends Component> navigationTarget, RouteParameters routeParameters, QueryParameters queryParameters)
      Resolves the page title of the given navigation target without creating an instance of it.

      The title is resolved in this order:

      1. the per-route DynamicPageTitle generator;
      2. the application-wide default PageTitleGenerator;
      3. the static PageTitle.value().
      The generators are obtained from the current VaadinService; when no service is active, only annotation-based resolution is performed.

      Since the navigation target is not instantiated, HasDynamicTitle.getPageTitle() is not consulted; the title of an instantiated, currently shown route may therefore differ from the result of this method. This is the stateless title resolution used to render navigation aids such as breadcrumbs and menus, for example over the entries of RouteConfiguration.getRouteHierarchy(Class, RouteParameters).

      Parameters:
      navigationTarget - the navigation target to resolve the title for, not null
      routeParameters - the route parameters the target is resolved with, not null
      queryParameters - the query parameters the target is resolved with, not null
      Returns:
      the resolved title, or an empty Optional if the target declares no title and no default generator is available
      Since:
      25.2
    • resolvePageTitle

      public Optional<String> resolvePageTitle(Component navigationTarget, RouteParameters routeParameters)
      Resolves the page title of the given navigation target instance, using empty query parameters.
      Parameters:
      navigationTarget - the navigation target instance to resolve the title for, not null
      routeParameters - the route parameters the target is resolved with, not null
      Returns:
      the resolved title, or an empty Optional if the target declares no title and no default generator is available
      Since:
      25.2
      See Also:
    • resolvePageTitle

      public Optional<String> resolvePageTitle(Component navigationTarget, RouteParameters routeParameters, QueryParameters queryParameters)
      Resolves the page title of the given navigation target instance.

      The title is resolved in this order:

      1. HasDynamicTitle.getPageTitle(), when the instance implements HasDynamicTitle and returns a non-null title — this matches the title shown when the route is actually navigated to;
      2. the per-route DynamicPageTitle generator;
      3. the application-wide default PageTitleGenerator;
      4. the static PageTitle.value().
      Unlike resolvePageTitle(Class, RouteParameters, QueryParameters), which cannot create an instance and therefore skips HasDynamicTitle, this overload mirrors the resolution performed during navigation.
      Parameters:
      navigationTarget - the navigation target instance to resolve the title for, not null
      routeParameters - the route parameters the target is resolved with, not null
      queryParameters - the query parameters the target is resolved with, not null
      Returns:
      the resolved title, or an empty Optional if the target declares no title and no default generator is available
      Since:
      25.2
    • handleExceptionNavigation

      public int handleExceptionNavigation(UI ui, Location location, Exception exception, NavigationTrigger trigger, tools.jackson.databind.node.BaseJsonNode state)
      Render error view for exception. Finds view to render based on the exception type. Exception view is chosen for matching exception and if no match is found choose by extended type.

      For internal use only. May be renamed or removed in a future release.

      Parameters:
      ui - current UI instance
      location - target location for failing navigation
      exception - exception thrown
      trigger - navigation trigger
      state - navigation state info
      Returns:
      the HTTP status code to return to the client if handling an initial rendering request
      Since:
      25.0.5
    • executeNavigation

      public int executeNavigation(UI ui, Location location, NavigationEvent navigationEvent, NavigationHandler handler, IntConsumer onSuccess)
      Execute navigation with a pre-resolved handler and optional success callback.

      This method handles the common navigation pattern of setting navigation state, executing the handler, running a success callback, and properly handling exceptions via handleExceptionNavigation(com.vaadin.flow.component.UI, com.vaadin.flow.router.Location, java.lang.Exception, com.vaadin.flow.router.NavigationTrigger, tools.jackson.databind.node.BaseJsonNode).

      For internal use only. May be renamed or removed in a future release.

      Parameters:
      ui - the UI to update, not null
      location - the navigation location, not null
      navigationEvent - the navigation event to pass to the handler
      handler - the navigation handler to execute
      onSuccess - optional callback to run after successful navigation (before clearing navigation state), may be null
      Returns:
      the HTTP status code resulting from the navigation
      Since:
      25.0.5
    • getRegistry

      public RouteRegistry getRegistry()
    • getErrorNavigationTarget

      public Optional<ErrorTargetEntry> getErrorNavigationTarget(Exception exception)
      Get a registered navigation target for given exception.
      Parameters:
      exception - exception to search error view for
      Returns:
      optional error target entry corresponding to the given exception
      Since:
      1.3