Pass data between Routes or Views

Hi,

how to pass an POJO between views or routes? For Example; I want to pass the UserInfo POJO from Login View to Home View. Could someone share the best practice and how to do it? Thanks.

There are multiple approaches. If you’re using a dependency injection framework like Spring or CDI, this could be a normal injection case. Another option could be using an event bus. You could also use the ComponentUtils helper to map the instance to the current UI object with
ComponentUtil.setData(UI.getCurrent(), YourClass.class, yourClassInstance);
and
ComponentUtil.getData(UI.getCurrent(), YourClass.class);

Yet another option could be using the current VaadinSession instance.

Thanks Olli. I was using VaadinSession yesterday but I moved today to use Spring Injection method. Its good to know we have something like ComponentUtils as well. Thanks.

I really like the new ComponentUtils option, it gives me more options for my use case. So when have people found it better to use VaadinSession as opposed to ComponentUtils? I know there is no right or wrong way, so long as you use it correctly. Just curious about how people have used both, with the addition of ComponentUtils.

ComponentUtils is a new addition to Flow, I think mostly to help with migration from Vaadin 7 and 8. Session is typically and historically what you use to store things like login information. That way, you can be sure that when you log out (=invalidate the session), there won’t be any lingering user information available.

Sounds good. BTW, ComponentUtils is perfect for certain use cases outside of this migration use case, so hopefully it won’t be removed. For instance, in our screwy case, I need certain pieces of data stored per browser tab/window. So I guess we have these basic use cases:

  1. ComponentUtils for Vaadin 7/8 conversion work
  2. ComponentUtils when you HAVE to have variable per browser tab or window
  3. All other cases, where data can be shared between browser tabs/windows, use VaadinSession ( since VaadinSession seems to normally be local to the browser session ).

Any concerns with the above, or warnings?

Sounds fine. You can also use a UI-scoped Bean for browser-tab scoped things if you’re using a DI framework like Spring or CDI.

Olli Tietäväinen:
There are multiple approaches. If you’re using a dependency injection framework like Spring or CDI, this could be a normal injection case. Another option could be using an event bus. You could also use the ComponentUtils helper to map the instance to the current UI object with
ComponentUtil.setData(UI.getCurrent(), YourClass.class, yourClassInstance);
and
ComponentUtil.getData(UI.getCurrent(), YourClass.class);

Yet another option could be using the current VaadinSession instance.

Hi Olli,

I understand the ComponentUtil way of doing this.
But, how could spring be used for this?

Imagine I have a master page where Item’s are listed, and if the user clicks on it, he would navigate to a detail page.
In the Detail page i want to know which item was clicked.

Doing a ComponentUtil.setData in the masterview, and retrieving it using ComponentUtil.getData on the detail page I understand, but how can spring be used for this?
Or do you mean specifically for LoginView (as mentioned in the original post), as the value is retrieved from the SecurityContext and not something that was explicitely set by the user.

Thank you,
kristof

This ComponentUtil.getData(UI.getCurrent(), YourClass.class) returns null to me. In Framework 8 it was easy to obtain a view instance by Injecting CDIViewProvider and getting the instance like this.

@Inject
private CDIViewProvider viewProvider;
...
viewProvider.getView("name-of-view");

CDI has changed in Platform 14 so that service is not available. I have tried many different alternatives using BeanProvider (Deltaspike) but no luck.

Any ideas on why I could be getting null value or how to properly use BeanProvider to get the instance of a Route/View?