Class NavigationEndedEvent

java.lang.Object
java.util.EventObject
com.vaadin.flow.router.NavigationEndedEvent
All Implemented Interfaces:
Serializable

public class NavigationEndedEvent extends EventObject
Event fired through the service event bus when the server has finished handling a navigation for a UI, telling how it ended.

It is always fired after the matching NavigationStartedEvent, on the same thread, also when the navigation throws. A listener can therefore keep timing state in a ThreadLocal. Both events are fired on the request thread while the session is locked. The listeners of this event are called in the reverse order of registration, so that they nest around the listeners of the started event.

Only the outermost navigation is reported. A forward, a reroute, the redirect that adds or removes a trailing slash and the rendering of an error view are part of the navigation that caused them and fire no events of their own. Their effect is in the outcome.

Resuming a postponed navigation with BeforeLeaveEvent.ContinueNavigationAction.proceed() is reported as a navigation of its own. Showing a PreserveOnRefresh view once the browser has sent its window name, in the request after the one reported as NavigationEndedEvent.NotShown, fires no events, since it continues a navigation that has already been reported.

Use a switch over the outcome to tell the cases apart:

 String result = switch (event.getOutcome()) {
 case Completed completed -> completed.navigationTarget().getSimpleName();
 case Postponed postponed -> "postponed";
 case Failed failed -> "failed: " + failed.error();
 case NotShown notShown -> "no view shown";
 };
 
Since:
25.4
See Also:
  • Constructor Details

    • NavigationEndedEvent

      public NavigationEndedEvent(UI ui, Location location, NavigationTrigger trigger, NavigationEndedEvent.Outcome outcome, int statusCode)
      Creates a new event.
      Parameters:
      ui - the UI that navigated, not null
      location - the requested location, not null
      trigger - the action that triggered the navigation, not null
      outcome - how the navigation ended, not null
      statusCode - the HTTP status code of the navigation, or -1 if the navigation threw
  • Method Details

    • getUI

      public UI getUI()
      Gets the UI that navigated.
      Returns:
      the UI, never null
    • getLocation

      public Location getLocation()
      Gets the location that was requested, before any forward or reroute. It is the same as in the matching NavigationStartedEvent.
      Returns:
      the requested location, never null
    • getTrigger

      public NavigationTrigger getTrigger()
      Gets the action that triggered the navigation. It is the same as in the matching NavigationStartedEvent.
      Returns:
      the navigation trigger, never null
    • getOutcome

      public NavigationEndedEvent.Outcome getOutcome()
      Gets how the navigation ended.
      Returns:
      the outcome, never null
    • getStatusCode

      public int getStatusCode()
      Gets the HTTP status code of the navigation, such as 200 for a view that was shown or 404 for the "not found" error view. On the initial page load it is the status code of the response. A navigation that throws has no status code, which is reported as -1.
      Returns:
      the HTTP status code, or -1 if the navigation threw