Setting content of my `UI` subclass in Vaadin Flow web app

亡梦爱人 提交于 2019-12-24 04:11:30

问题


In Vaadin Flow, writing a subclass of UI class is no longer necessary. Yet the page of the manual on Differences Between V10 and V8 Applications suggests we are free to do so.

The problem: The UI class in Flow has no UI::setContent method.

This usual line of code in our UI::init method fails in Flow:

this.setContent( layout );  // <--- No method `setContent` found in Flow

➥ How do we set the content to be displayed within our UI subclass at runtime?

Here is my code, with the line of setContent that fails.

package com.acme;

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

import javax.servlet.annotation.WebServlet;

public class MyUI extends UI {
    protected void init ( VaadinRequest request ) {
        VerticalLayout layout = new VerticalLayout();
        this.setContent( layout );
    }

    @WebServlet (
        urlPatterns = "/*",
        name = "myservlet",
        asyncSupported = true
    )
    // The UI configuration is optional
    @VaadinServletConfiguration (
        ui = MyUI.class,
        productionMode = false
    )
    public class MyServlet extends VaadinServlet {
    }
}

回答1:


The UI is a component itself and implements HasComponents. Hence, you can simply call add(Component...) method to fill it with components.



来源:https://stackoverflow.com/questions/53271385/setting-content-of-my-ui-subclass-in-vaadin-flow-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!