3.5.12.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
@Route
annotation 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; -
parentPrefix
is used for routes squashing (see below). -
root
is 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 optionallyrouteParentPrefix
equivalent to theparentPrefix
parameter,rootRoute
equivalent to theroot
parameter) 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
Order
entity:@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
orders
route in the URL when the editor screen is opened right after browser. Just specify the repeated part in theparentPrefix
parameter of the@Route
annotation 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
UrlRouting
bean 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 asNavigationState
object.
The
pushState()/replaceState()
methods accept the current screen controller and optional map of parameters.See an example of using
UrlRouting
in the section below. -