Confused about navigation parameters

In my AppLayout drawer I have a SideNav with a series of SideNavItems, that should all point to the same TargetView, but with different parameters.

TargetView implements HasUrlParameter<Integer>
This means it expects an url like “/target/42”

This works, but I’d prefer not to hard-code the path:

var item = new SideNavItem(“Number 42”, “/target/42”);

I can give the target class instead, with one or more RouteParm, but they seem to only support named parameters. Ie:

item = new SideNavItem(“Number 42”, TargetView.class, new RouteParm(“id”, 42));

That would give URL /target/id/42 ?
At least it fails at startup because it can’t find a view with matching parameters.

I did finally notice that UI.navigate has a version that takes a single value as parameter, and it wraps it with HasUrlParameterFormat.getParameters.

Using that with my SideNavItem it works, but javadoc on HasUrlParameterFormat says it is for internal use only.

Am I missing something?

I personally would always suggest to use https://vaadin.com/docs/latest/flow/routing/additional-guides/route-templates - those also have matching constructors with the sidebar. Your parameter do not have such privilege

Thanks. That worked.
It does mean I have to hard-code more strings (the parameter name) and convert the value, so it is a bit cumbersome. Maybe the added flexibility will pay off later.

var parm = new RouteParam("id", 42);
var item = new SideNavItem("Number 42", TargetView.class, new RouteParameters(parm));


@Route(value="target/:id")
public class TargetView extends VerticalLayout implements BeforeEnterObserver {

    @Override
    public void beforeEnter(BeforeEnterEvent event) {
        id = Integer.parseInt(event.getRouteParameters().get("id").get());
    }

Alternative: you could also use query parameter… but a key/value pair is always there to be remembered somewhere if you don’t wanna create routes for each possible value

First I would create constants for the parameters and then you can create a Helper or a base view that hides the handling of the parameters.