首页 > 解决方案 > 使用手册的示例代码在 Vaadin Flow 中编写“UI”子类

问题描述

在 V10 和 V8 应用程序之间的差异手册的页面上,有这个示例代码供那些想要像在 Vaadin 8 中那样编写子类的人使用,尽管在Vaadin FlowUI中不再需要。

(将原来的 mydomain-dot-com 更改example.com为安抚 Stack Overflow 审查机器人)

@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
    }
}   

从语法上讲,这要么是不正确的,要么是要写入两个单独的.java文件中。

还是应该像在 Vaadin 8 中默认那样在MyServlet类中设置类?MyUI像这样:

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 {
    }
}

标签: javaweb-applicationsviewvaadinvaadin-flow

解决方案


该示例旨在写入两个单独的 .java 文件。或者,您可以将 servlet 定义为公共静态内部类,包含在 UI 类中。在这种情况下,ui属性@VaadinServletConfiguration默认为封闭的 UI。


推荐阅读