Search in sources :

Example 6 with NavigationState

use of com.vaadin.flow.router.NavigationState in project flow by vaadin.

the class NavigationStateRenderer method notifyNavigationTarget.

@Override
protected void notifyNavigationTarget(Component componentInstance, NavigationEvent navigationEvent, BeforeEnterEvent beforeEnterEvent, LocationChangeEvent locationChangeEvent) {
    if (!(componentInstance instanceof HasUrlParameter)) {
        return;
    }
    NavigationState navigationState = getNavigationState();
    Class<? extends Component> routeTargetType = navigationState.getNavigationTarget();
    List<String> parameters = navigationState.getUrlParameters().orElse(null);
    Object deserializedParameter = null;
    try {
        deserializedParameter = ParameterDeserializer.deserializeRouteParameters(routeTargetType, parameters);
    } catch (Exception e) {
        beforeEnterEvent.rerouteToError(NotFoundException.class, String.format("Failed to parse url parameter, exception: %s", e));
        return;
    }
    HasUrlParameter<Object> hasUrlParameter = (HasUrlParameter<Object>) componentInstance;
    hasUrlParameter.setParameter(beforeEnterEvent, deserializedParameter);
}
Also used : NavigationState(com.vaadin.flow.router.NavigationState) NotFoundException(com.vaadin.flow.router.NotFoundException) NotFoundException(com.vaadin.flow.router.NotFoundException) HasUrlParameter(com.vaadin.flow.router.HasUrlParameter)

Aggregations

NavigationState (com.vaadin.flow.router.NavigationState)6 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)4 Location (com.vaadin.flow.router.Location)4 UI (com.vaadin.flow.component.UI)3 ErrorParameter (com.vaadin.flow.router.ErrorParameter)3 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)3 MockUI (com.vaadin.tests.util.MockUI)3 Test (org.junit.Test)3 HasErrorParameter (com.vaadin.flow.router.HasErrorParameter)2 NotFoundException (com.vaadin.flow.router.NotFoundException)2 HasElement (com.vaadin.flow.component.HasElement)1 HasUrlParameter (com.vaadin.flow.router.HasUrlParameter)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 ErrorStateRenderer (com.vaadin.flow.router.internal.ErrorStateRenderer)1 ApplicationRouteRegistry (com.vaadin.flow.server.startup.ApplicationRouteRegistry)1 JsonObject (elemental.json.JsonObject)1