Work in progress
URL generation
Router exposes methods to get the navigation URL for registered navigation targets.
For a normal navigation target the request is a simple as calling for Router.getUrl(Class target)
Java
@Route("path")
public class PathComponent extends Div {
public PathComponent() {
setText("Hello @Route!");
}
}
public class Menu extends Div {
public Menu() {
String route = UI.getCurrent().getRouter()
.getUrl(PathComponent.class);
Anchor link = new Anchor(route, "Path");
add(link);
}
}
The returned url would in this case simply be resolved to path, but in the case where we have parent layouts that add path parts it might not be as simple to generate the path by hand.
URL generation for navigation target with parameter
For navigation targets with required parameters the parameter is given to resolver and the returning string will then contain the parameter e.g. Router.getUrl(Class target, T parameter)
Java
@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 = UI.getCurrent().getRouter()
.getUrl(GreetingComponent.class, "anonymous");
Anchor link = new Anchor(route, "Greeting");
add(link);
}
}