Vaadin 13 Flow HasUrlParameter.setParameter

Colleagues, I try to understand with Vaadin Routers. The documentation https://vaadin.com/docs/v13/flow/routing/tutorial-router-url-parameters.html literally says that the setParameter method should be called by the router before the route itself is activated. I observe exactly the opposite situation. The setParameter method is called after the constructor (which, in principle, can be understood), after the initialization method marked with the @PostConstruct annotation … Moreover, although the setParameter method is called in the passed parameter, it is empty !! ?? I give the text of the classes below:

@Route(“first”)
public class First extends Div {
public First() {
NativeButton button = new NativeButton(“click me”);
button.addClickListener(e → {
Map<String, String> map = new HashMap<>();
map.put(“id”, “Hello”);
QueryParameters parameters = QueryParameters.simple(map);
button.getUI().ifPresent(ui → ui.navigate(“second”, parameters));
});
add(button); }
}

@Route(“second”)
public class Second extends Div implements HasUrlParameter {
public Second(){
}
@PostConstruct
void init(){
}
@Override public void setParameter(BeforeEvent event, String parameter) {
setText("Parameter " + parameter);
}
}

Thanks in advance)

Two things that I noticed

  • You did not define String as the HasUrlParameter-type
  • You do not pass a String as parameter in ui.navigate. You do not need to create a QueryParameters object, just pass the String.
button.addClickListener(e -> { 
	//button.getUI().ifPresent(ui -> ui.navigate("second", "Hello"));  
	// Edit:
	button.getUI().ifPresent(ui -> ui.navigate(Second.class, "Hello")); 
}); 
@Route("second")
public class Second extends Div implements HasUrlParameter<String> {...}

Caspar, thanks for the reply.
However, I must note that the specification of the ui.navigate method (string, string) is missing.
This format of the Navigate method helped:
void navigate (Class <? Extends C> navigationTarget, T parameter)

Unfortunately, the question of using the navigate method (String location, QueryParameters queryParameters) was left unsolved for me)

Thanks so much for helping me figure it out.