在Vaadin Flow网络应用程序中设置我的UI子类的内容
在Vaadin Flow中,不再需要编写UI
类的子类.但是,该手册的页面在 V10与V8应用程序 建议我们自由地这样做.
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.
问题: UI
Flow中的类没有UI::setContent
方法.
The problem: The UI
class in Flow has no UI::setContent
method.
我们的UI::init
方法中的这行常规代码在Flow中失败:
This usual line of code in our UI::init
method fails in Flow:
this.setContent( layout ); // <--- No method `setContent` found in Flow
➥我们如何设置在运行时在UI
子类中显示的内容?
➥ How do we set the content to be displayed within our UI
subclass at runtime?
这是我的代码,其中setContent
行失败.
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 {
}
}
UI
本身是一个组件,并实现HasComponents
.因此,您只需调用add(Component...)
方法以将其填充组件即可.
The UI
is a component itself and implements HasComponents
. Hence, you can simply call add(Component...)
method to fill it with components.