URL Generation

Router exposes methods to get the navigation URL for registered navigation targets.

Standard Navigation Targets

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

Example: Returned URL 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.

To access the URL of a navigation target registered for a route which contains parameters, the values of the parameters are required in order to generate the URL. The URL 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 URL 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);
    }
}

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

Example: Returning a URL string containing a parameter: 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);
    }
}