URL Generation
- Standard Navigation Targets
- Navigation Target With URL Templates
- Navigation Target With HasUrlParameter
Router
exposes methods to get the navigation URL of registered navigation targets.
Standard Navigation Targets
For standard navigation targets, the request is a simple call for 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.
Navigation Target With URL Templates
To access the URL of a navigation target registered for a route which contains parameters, the values of the parameters are required 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: In case 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 will succeed.
@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);
}
}
Navigation Target With HasUrlParameter
For navigation targets with required parameters, the parameter is given to the resolver and the returning string contains the parameter.
Example: Returning string contains 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);
}
}
F488FCC1-28B4-4222-ADD9-B67B5A4E8354