How to correctly extend a UI

So, I have a custom UI but to tell vaadin to use it I had to also create a custom Servlet. My servlet looks like this.

@WebServlet(urlPatterns = "/*", name = "CustomServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = CustomUI.class, productionMode = false)
public class CustomServlet extends VaadinServlet {

My question is: Is this correct ? Because the annotation @VaadinServletConfiguration is deprecated in Vaadin 14.

Defining your own UI class is discouraged starting from Vaadin 10. If you still want to do it, then your described way is good.

With Vaadin 8 or older, it was required to have a custom UI class because that’s where you would build the initial contents of the UI. With Vaadin 10 and newer, this is instead handled by the router based on annotated classes discovered on the classpath. Furthermore, any “global” logic can be defined in the application’s main layout component instead of in the UI instance.

I would like to understand why you want to have your custom UI class, since it implies that we may need to add some functionality to make it easier to not have one.

Couple of ideas from some projects - the custom UI could hold:

  • An UI-scoped background task Executor;
  • ExtendedClientDetails which can be obtained immediately
  • browserwidth and height, initially got from ExtendedClientDetails but then updated via addBrowserWindowResizeListener
  • The go-to single place to configure security redirects, possibly EULA redirects, possibly first time welcome wizard redirects…
  • Possibly other UI- (browser-tab-) scoped stuff

Of course you can add the UI init listener and create a separate utility class and store it into the UI instance via ComponentUtil.setData(UI.getCurrent(), Util.class, util), then destroy the class/executor by using the UI detach listener. If this is the preferred way to do things, it should be documented.