Introduction 📝
The router compares the current URL to the established routes when you launch your application. The routes, in turn, are in charge of displaying templates, loading data, and configuring the application state. In this article, we will discuss how to define Routes and how to Navigating Between Routes along with Route parameters.

Registering Routes Dynamically
You may add and remove routes dynamically at runtime in addition to registering routes and route templates using the @Route annotation. This is helpful, for instance, when a route needs to be added or withdrawn due to modified business data or starting program setup.
You may restrict route access using the RouteConfiguration class to:
-
the whole user base of the program, or
-
only a select few users are actively using the session scope.
The static methods forSesionScope() and forApplicationScope() from the RouteConfiguration class allow you to access the scope.
🔖Configuring User-Specific Routes
You can configure user-specific routes by adding and removing routes for certain users; for example, based on their access rights.
RouteConfiguration.forSessionScope()
.setRoute(
"admin",
AdminView.class
);
RouteConfiguration.forSessionScope()
.setRoute(
"home",
HomeView.class,
MainLayout.class
);
A unique user's session scope can override any registered @Route in the application scope.
Only while the session is active can the current user access the routes in the session scope. The session-scoped routes are no longer accessible when the user logs out, invalidating the session. There is no need to delete these routes manually.
🔖Removing Routes
Just like we had added some permission to users we can also remove them. You must specify precisely which route to delete when eliminating routes.
Example:
1. Removing a navigation target (AdminView.class) along with all of its registered route aliases and route templates.
RouteConfiguration configuration = RouteConfiguration
.forSessionScope();
configuration.removeRoute(AdminView.class);
2. Removing a path ("admin"), which only removes the target mapped to it.
configuration.removeRoute("admin");
Remarks:
- The application-scoped route becomes accessible once more once a route in the session scope that had previously replaced a route in the application scope is removed.
-
Any class annotations are disregarded when dynamically registering a route, with the exception of methods that contain Annotated, like setAnnotatedRoute()
For further related information, see:
🔖Adding Routes on Application Startup
In the above section, we see how to add and remove the routes. Now let's try to Adding Routes to Application Startup
Example: Registering a route while deploying using ServiceInitLister.
public class ApplicationServiceInitListener
implements VaadinServiceInitListener {
@Override
public void serviceInit(ServiceInitEvent event) {
if (!event.getSource()
.getDeploymentConfiguration()
.isProductionMode()) {
RouteConfiguration configuration =
RouteConfiguration.forApplicationScope();
configuration.setRoute(
"crud",
DBCrudView.class
);
}
}
}🔖Getting Registered Routes and Listening for Changes
You might need to change UI elements, including navigation menus, based on the added or withdrawn routes when routes are registered dynamically.
The getAvailableRoutes() function of the RouteConfiguration allows you to get the registered route templates. Using the addRoutesChangeListener() function, you can set up a listener to receive notifications of route changes.
Example: Obtaining available routes and registering routes switch listeners.
RouteConfiguration configuration = RouteConfiguration
.forSessionScope();
configuration.getAvailableRoutes()
.forEach(menu::addMenuItem);
configuration.addRoutesChangeListener(event -> {
event.getAddedRoutes().stream()
.filter(route -> route instanceof RouteData)
.forEach(menu::addMenuItem);
event.getRemovedRoutes().stream()
.filter(route -> route instanceof RouteData)
.forEach(menu::removeMenuItem);
});🔖Adding Route Aliases for Dynamic Routes
For adding dynamic routes, the first route for which a navigation target is added is designated as the primary route, which can be obtained using the RouteConfiguration getUrl() methods.
A route alias is any additional registered route.
RouteConfiguration configuration =
RouteConfiguration.forSessionScope();
configuration.setRoute("main", MyRoute.class);
configuration.setRoute("info", MyRoute.class);
configuration.setRoute("version", MyRoute.class);
The configuration.getUrl(MyRoute.class) function returns main in this case.
Example: The previous route registration example's static class definition is equivalent.
@Route("main")
@RouteAlias("info")
@RouteAlias("version")
private class MyRoute extends Div {
}
If the "primary" path is eliminated but an alias path remains, the first alias in the register becomes the main path.
🔖Dynamic Registration of @Route Annotated Classes
You may configure the routes statically but postpone registration until runtime if you wish to map all routes, in a similar manner, using the @Route annotation.
Add the registerAtStartup = false argument to the @Route annotation to bypass static registration to the application-scoped registry on startup. This also makes it easy to leverage existing parent chains and change pathways from the parent.
Example: To postpone route registration, use the registerAtStartup argument.
@Route(value = "quarterly-report",
layout = MainLayout.class,
registerAtStartup = false)
@RouteAlias(value = "qr", layout = MainLayout.class)
public class ReportView extends VerticalLayout
implements HasUrlParameter<String> {
}
if (getCurrentUser().hasAccessToReporting()) {
RouteConfiguration.forSessionScope()
.setAnnotatedRoute(ReportView.class);
}
In the above example, we had seen the use of the registerAtStartup argument. You can also see the example of a sample project by clicking here Adding a New View on User Log-in.






