Using manual’s example code for writing a `UI` subclass in Vaadin Flow

Viewed 204

On the page of the manual for Differences Between V10 and V8 Applications, there is this example code for those who want to write a UI subclass as we did in Vaadin 8, despite being no longer required in Vaadin Flow.

(changing the original mydomain-dot-com to example.com to appease the Stack Overflow censor robot)

@WebServlet(urlPatterns = "/*", name = "myservlet", asyncSupported = true,
// Example on initialization parameter configuration
initParams = {
        @WebInitParam(name = "frontend.url.es6", value = "http://example.com/es6/"),
        @WebInitParam(name = "frontend.url.es5", value = "http://example.com/es5/") })
// The UI configuration is optional
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public class MyServlet extends VaadinServlet {
}

// this is not necessary anymore, but might help you get started with migration
public class MyUI extends UI {
    protected void init(VaadinRequest request) {
        // do initial steps here.
        // previously routing
    }
}   

Syntactically that is either incorrect or is meant to be written into two separate .java files.

Or should the MyServlet class be set within the MyUI class, as was done by default in Vaadin 8? Like this:

package com.raddkit;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinServlet;
import com.vaadin.flow.server.VaadinServletConfiguration;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

public class MyUI extends UI {
    protected void init ( VaadinRequest request ) {

    }

    @WebServlet ( urlPatterns = "/*", name = "myservlet", asyncSupported = true,
        // Example on initialization parameter configuration
        initParams = {
            @WebInitParam ( name = "frontend.url.es6", value = "http://example.com/es6/" ) ,
            @WebInitParam ( name = "frontend.url.es5", value = "http://example.com/es5/" ) } )
    // The UI configuration is optional
    @VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
    public class MyServlet extends VaadinServlet {
    }
}
1 Answers

That example is meant to be written into two separate .java files. Alternatively, you can define the servlet as a public static inner class, enclosed in a UI class. In that case, the ui attribute of @VaadinServletConfiguration defaults to the enclosing UI.

Related