3.5.13.2. Routing API
This section describes the key concepts of the routing API.
- Route Registration
-
In order to register a route for a screen, add the
@Routeannotation to the screen controller, for example:@Route("my-screen") public class MyScreen extends Screen { }The annotation has three parameters:
-
path(orvalue) is the route itself; -
parentPrefixis used for routes squashing (see below). -
rootis the boolean property which enables to specify whether a route is registered for the root screen (like Login screen or Main screen). The default value isfalse.
If you need to define a route for a legacy screen add the
route(and optionallyrouteParentPrefixequivalent to theparentPrefixparameter,rootRouteequivalent to therootparameter) attribute to the screen’s element in the screens.xml file, for example:<screen id="myScreen" template="..." route="my-screen" /> -
- Route Squashing
-
This feature is designed to keep the URL clean and readable when opening multiple screens with routes having the same parts.
Suppose that we have browser and editor screens for the
Orderentity:@Route("orders") public class OrderBrowser extends StandardLookup<Order> { } @Route("orders/edit") public class OrderEditor extends StandardEditor<Order> { }URL squashing is used to avoid repeating of the
ordersroute in the URL when the editor screen is opened right after browser. Just specify the repeated part in theparentPrefixparameter of the@Routeannotation on the editor screen:@Route("orders") public class OrderBrowser extends StandardLookup<Order> { } @Route("orders/edit", parentPrefix = "orders") public class OrderEditor extends StandardEditor<Order> { }Now when the editor is opened in the same tab as the browser, the resulting address will be like
app/#main/0/orders/edit?id=…
- Mapping of UI State to URL
-
The
UrlRoutingbean allows you to change the current application URL according to the current screen and some parameters. It has the following methods:-
pushState()– changes the address and pushes new browser history entry; -
replaceState()– replaces the address without adding new browser history entry; -
getState()– returns a current state asNavigationStateobject.
The
pushState()/replaceState()methods accept the current screen controller and optional map of parameters.See an example of using
UrlRoutingin the section below. -