Docs

Documentation versions (currently viewingVaadin 24)

Menu Registry

Collecting menu annotated routes for menu generation.

To generate a custom menu there’s the MenuRegistry for collecting defined server and client routes. Client routes are collected from the FileSystem routes definition. Server routes are collected for routes annotated with @Menu.

The collected menu items are filtered by access control from the UI (NavigationAccessControl and ViewAccessChecker). If the application has login and route defined Roles, the returned list is filtered for only available routes.

Creating Routes Menu

For a Flow application, any route annotated with @Menu is eligible for automatic collection.

To get the available menu routes, call the static method MenuRegistry.collectMenuItems(). This returns a Map<String, AvailableViewInfo>. The key in the returned map is the route path that should be used for navigation. It would make creating a simple menu using Anchor look like this:

Map<String, AvailableViewInfo> menuItems = MenuRegistry.collectMenuItems();
menuItems.forEach((path, info) -> layout.add(new Anchor(path, info.menu().title())));

Only Server Routes for Hybrids

For a hybrid application in which only server menu items are wanted, there’s the static method collectAndAddServerMenuItems. It takes in the RouteConfiguration to use, and a Map into which to populate the routes.

This could be called as follows:

Map<String, AvailableViewInfo> menuRoutes = new HashMap<>();
MenuRegistry.collectAndAddServerMenuItems(RouteConfiguration.forApplicationScope(), menuRoutes);

Creating Client Routes Menu on Server

For client routes, the same static method MenuRegistry.collectMenuItems() can also be used to get server routes.

Only Client Routes for Hybrids

To collect only the client routes, MenuRegistry has the method getClientRoutes(boolean filterClientViews, AbstractConfiguration configuration). The boolean filterClientViews is used to define whether the accessible client routes should be removed from the result. The required configuration can be gotten from the VaadinService method, getDeploymentConfiguration().

For a full list, there’s also the shorthand method getClientRoutes() in FrontendUtils. For creating a client menu, see Creating Menu From Routes.

Data Returned for Menu Items

The AvailableViewInfo for the route contains information on the route. Using this data, it’s possible to populate a menu, automatically.

The data contained is as follows:

title

The @PageTitle if defined; else the class simple name. For the client, title is populated from the FS router data.

rolesAllowed

Array of roles that are allowed for the item.

loginRequired

Whether menu route requires a login to be viewed.

route

Route for the view. On the server this should match the array key. For client routes, though, this may be missing parent path parts, if any.

lazy

Not in use. Later marks the route as lazy loaded from a different bundle.

register

Whether route should be registered automatically into a menu.

menu

MenuData on the @Menu. See below for menu definitions.

children

Hierarchy information on routes below the given view. For client routes only.

routeParameters

Defines any route parameters and their RouteParamType

Data in MenuData:

title

Title set in the annotation or the same as AvailableViewInfo.title.

order

Order number for the menu item if defined.

exclude

Whether item should be excluded for the automatic menu.

icon

Icon to use in the menu. The value can be entered inside a <vaadin-icon> element’s icon attribute. This accepts icon group and a name like vaadin:file. Or the value can be given in <vaadin-icon> element’s src attribute, which takes the path to the icon (e.g., line-awesome/svg/lock-open-solid.svg).

647922A8-D542-4FA7-AAF6-44D40FC6A33B