Documentation

Documentation versions (currently viewingVaadin 23)
Check out the new styling guides

Retrieving Routes

The class RouteConfiguration exposes methods to get the generated route for registered navigation targets.

Standard Navigation Targets

For standard navigation targets, the request is a simple call to RouteConfiguration.forSessionScope().getUrl(Class target).

Example: The generated route is resolved to path.

@Route("path")
public class PathComponent extends Div {
    public PathComponent() {
        setText("Hello @Route!");
    }
}

public class Menu extends Div {
    public Menu() {
        String route = RouteConfiguration.forSessionScope()
                .getUrl(PathComponent.class);
        Anchor link = new Anchor(route, "Path");
        add(link);
    }
}

If parent layouts add route prefixes, it is not always simple to generate the path by hand.

For navigation targets with required route parameters, passing the parameter to the resolver returns a string containing the parameter.

Example: Returning a route containing a parameter with RouteConfiguration.forSessionScope().getUrl(Class target, T parameter).

@Route(value = "greet")
public class GreetingComponent extends Div
        implements HasUrlParameter<String> {

    @Override
    public void setParameter(BeforeEvent event, String parameter) {
        setText(String.format("Hello, %s!", parameter));
    }
}

public class ParameterMenu extends Div {
    public ParameterMenu() {
        String route = RouteConfiguration.forSessionScope()
                .getUrl(GreetingComponent.class, "anonymous");
        Anchor link = new Anchor(route, "Greeting");
        add(link);
    }
}

To access a route that contains template parameters, the values of the parameters are required in order to generate the route.

The route is generated by replacing the parameter placeholders with the actual values.

@Route(value = "item/:id([0-9]*)/edit")
public static class ItemEdit extends Component {
}

public class MenuView extends Div {

    private void addItemEditLink() {
        String url = routeConfiguration.getUrl(
                ComponentView.class,
                new RouteParameters("id", "123"));

        // The generated url is `item/123/edit`
        Anchor link = new Anchor(url, "Button Api");
        add(link);
    }
}
Note
The provided parameter values have to match the actual regex of the parameter template, otherwise the route generation will fail.

Example: If the navigation target is registered with more than one route, that is, alias routes, the parameter values are matched against all route templates starting with the main route, until one succeeds.

@Route(value = ":path*")
@RouteAlias(value = ":tab(api)/:path*")
@RouteAlias(value = ":tab(overview|samples|links|reviews|discussions)")
@RoutePrefix("component/:identifier")
public static class ComponentView extends Component {
}

public class MenuView extends Div {

    private void addButtonApiLink() {
        String url = routeConfiguration.getUrl(
                ComponentView.class,
                new RouteParameters(
                        new RouteParam("identifier", "button"),
                        new RouteParam("tab", "api"),
                        new RouteParam("path", "com/vaadin/flow/button")));

        // The generated url is `component/button/api/com/vaadin/flow/button`
        Anchor link = new Anchor(url, "Button Api");
        add(link);
    }
}

70FCF10A-3996-4EFD-9F58-D538D205A4BE