using RouteParams within Class

Hi, I have created following class:

@Route(“details/:bcSerial”)
public class Detail extends Composite implements BeforeEnterObserver {

String route;

@Override
public void beforeEnter(BeforeEnterEvent event) {
    route = event.getRouteParameters().get("bcSerial").get();
    System.out.println(route);
}

@Override
protected Component initContent() {
    System.out.println(route);
 }

}

When the route in beforeEnter Method gets printed, I have access to it. But in the initContentMethod, it is a null value again. What can I do to use the route value also in the initContent method?

Thx in advantage :slightly_smiling_face:

I have found out that initContent() will executed first - So how can i get access to the routeparameter?

Why not use it directly from the BeforeEnterObserver?
Something like this:
Detail.java (735 Bytes)

@Override
protected Component initContent() {
    var content = new Label();
    content.addAttachListener(event -> {
        content.setText(route);
    });
    return content;
}

Or something like this, if you want the ui code to stay together

Thank you - I thought, I have used it, did not see that I have used BeforeEnterEvent :man_facepalming:

Thank you for your suggestions, but unfortunately it does not solve my problem. Therefore here are some more background information:
I have a dashboard-page containing a grid, filled with BreadCrumbs (these are the objects, see Picture 1). There is also a detail-button for each BreadCrumb and if I click on this button, I get navigated to the detail page of this specific BreadCrumb. On the detail page, I want to see all detail (i.e. all properties / attributes) of the BreadCrumb.

I will copy the code of my detailclass underneath.
The BreadCrumbRepository is an ArrayList containing some BreadCrumbs.

public class Detail extends Composite<Component> implements BeforeEnterObserver{
    @Autowired
    BreadCrumbRepository bcRepository;
    
    BreadCrumb bc;
    
    String route;
    
    @Override
    public void beforeEnter(BeforeEnterEvent event){
        route = event.getRouteParameters().get("bcSerial").get();
        bc = bcRepository.getBreadCrumbBySerial(route);
        System.out.println(bc.getName()); //this works - I get the name of the BreadCrumb
    }
    
    @Override
    protected Component initContent(){
        System.out.println(bc.getName()); //throws a NullPointerException -> because this.bc is null
    }
}

Picture 1
image.png

Anything you do inside the navigation events IS not available for your initComponent method. (Except with cumbersome workarounds) You have to refactor your view. For example not using Composite because there is literally no added value using Composite on views. Instead build your layout for example in the navigation events or create the layout in such a way that it can take the value later

Thank you :slightly_smiling_face: now it’s working :smile: