Search code examples
parameter-passingvaadinvaadin8new-window

Pass information to new web-browser window/tab being opened with BrowserWindowOpener in Vaadin 8


In Vaadin 8, we can let the user open a new tab/window within their web browser by clicking a button that has been associated with a BrowserWindowOpener. As discussed in the manual, a new UI is instantiated on our behalf. All we pass is a .class object, the class of our UI subclass to be instantiated for display in the new window/tab. For example:

BrowserWindowOpener opener = new BrowserWindowOpener( PersonDetailUI.class );

That works for me. My question is: How do I pass some information to that new UI object in that new window/tab?

For example, I might need to pass:

  • The ID number or UUID of a record to be looked up in a database.
  • A JavaBean object ready for display in a layout.

I see I asked about this same issue for Vaadin 7. Consider this an updated version of the Question for Vaadin 8. The only Answer there speculated about adding parameters to the URI of the new window. But that limits me to a small piece of text. I prefer to pass a smart object rather than a dumb string.


Solution

  • There are basically three approaches you can use, and combinations of these

    1. Use URI parameters. As you mentioned, this is limited to String type data.

      • You can read the URI fragment in UI with e.g. String uriFragment = Page.getCurrent().getUriFragment();
      • You can read URI parameter using VaadinRequest.getParameter(), VaadinRequest is given as parameter in init(...) of main UI
    2. UI's in different browser tabs share the same Vaadin session. That gives some tools, namely

      • You can use session attributes, i.e. VaadinSession.getCurrent().getAttribute(…) and VaadinSession.getCurrent().setAttribute(…)

      • If you use CDI or Spring, you can Inject / Autowire @VaadinSessionScoped bean. The instance is then bound to Session and hence shared between the tabs.

    3. Read data from database (possibly using 1. and/or 2. as help for keys)