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

  1. @Route("path")
  2. public class PathComponent extends Div {
  3. public PathComponent() {
  4. setText("Hello @Route!");
  5. }
  6. }
  7. public class Menu extends Div {
  8. public Menu() {
  9. String route = UI.getCurrent().getRouter()
  10. .getUrl(PathComponent.class);
  11. Anchor link = new Anchor(route, "Path");
  12. add(link);
  13. }
  14. }

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

  1. @Route(value = "greet")
  2. public class GreetingComponent extends Div
  3. implements HasUrlParameter<String> {
  4. @Override
  5. public void setParameter(BeforeEvent event,
  6. String parameter) {
  7. setText(String.format("Hello, %s!", parameter));
  8. }
  9. }
  10. public class ParameterMenu extends Div {
  11. public ParameterMenu() {
  12. String route = UI.getCurrent().getRouter()
  13. .getUrl(GreetingComponent.class, "anonymous");
  14. Anchor link = new Anchor(route, "Greeting");
  15. add(link);
  16. }
  17. }