URL Routes Generator

Sometimes, it is necessary to get proper URL that can be sent via email or shown to the user. The simplest way to generate it is by using URL Routes Generator.

URL Routes Generator provides API for URL links generation:

  • Link to the editor of an entity instance.

  • Link to the screen by screen id or screen class.

  • Link to the screen with provided parameters.

The getRouteGenerator() method of UrlRouting bean allows you to get an instance of RouteGenerator. RouteGenerator has the following methods:

  • getRoute(String screenId) – returns a route for screen with given screenId, for example:

    String route = urlRouting.getRouteGenerator().getRoute("demo_Customer.browse");

    The result:

    route = "http://host:port/context/#main/customers"

  • getRoute(Class<? extends Screen> screenClass) – generates a route for screen with the given screenClass, for example:

    String route = urlRouting.getRouteGenerator().getRoute(CustomerBrowse.class);

    The result:

    route = "http://host:port/context/#main/customers"

  • getEditorRoute(Entity entity) – generates a route for default editor with the given entity, for example:

    Customer сustomer = customersTable.getSingleSelected();
    String route = urlRouting.getRouteGenerator().getEditorRoute(сustomer);

    The result:

    route == "http://localhost:8080/app/#main/customers/edit?id=5jqtc3pwzx6g6mq1vv5gkyjn0s"

  • getEditorRoute(Entity entity, Class<? extends Screen> screenClass) – generates a route for editor with the given screenClass and entity.

  • getRoute(Class<? extends Screen> screenClass, Map<String, String> urlParams) – generates a route for screen with the given screenClass and urlParams.

URL Routes Generator Example

Suppose that we have a Customer entity with standard screens that have registered routes. Let us add a button to the CustomerBrowse that generates a link to the selected entity with some parameters. This button invokes the generateRoute method:

private UrlRouting urlRouting;

private GroupTable<Customer> customersTable;

private Dialogs dialogs;

public void generateRoute() {
   Customer selectedCustomer = customersTable.getSingleSelected();
   if (selectedCustomer != null) {
      String routeToSelectedRole = urlRouting.getRouteGenerator()
         .getEditorRoute(selectedCustomer, ImmutableMap.of("someParam", "someValue"));

                    .withCaption("Generated route")

The resulting route looks like this:

url generate route