You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vaadin Flow中创建头部与主体均为空的空视图UI?

Got it, let's figure out how to create a totally empty Vaadin Flow UI where both the <head> and <body> are completely clean—no default scripts, styles, or auto-generated tags cluttering up the HTML. Here's what you need to do:

1. Create a Minimal View That Renders Raw Empty HTML

The key is to bypass Vaadin's default component rendering and directly output the exact empty HTML structure you want. Extend the Html component and pass your target HTML, then clear any leftover default content from the page:

@Route("") // Set this as your root route
public class EmptyHtmlView extends Html {

    public EmptyHtmlView() {
        // Pass the exact empty HTML structure you're targeting
        super("<html><head></head><body></body></html>");
        
        // Wipe out any remaining default head content Vaadin might add
        Page page = Page.getCurrent();
        page.getStyles().clear();
        page.getJavaScript().clear();
        page.getHead().getChildren().removeIf(child -> true);
    }
}

2. Disable Vaadin's Default Client Runtime Injections

Even with the above view, Vaadin might still inject its bootstrap scripts and styles under the hood. To stop this, adjust your app's configuration:

For Spring Boot projects

Add these properties to your application.properties file:

# Disable auto-loading of Vaadin's frontend resources
vaadin.whitelisted-packages=
vaadin.frontend.url=
vaadin.flow.embed-app-shell=false

For non-Spring Boot projects

Create a custom VaadinServlet to block default resource loading:

@WebServlet(urlPatterns = "/*", name = "EmptyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = EmptyHtmlView.class, productionMode = false)
public class EmptyUIServlet extends VaadinServlet {
    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
        getService().setClassLoader(null); // Prevent loading default frontend assets
    }
}

3. Clean Up App Shell Configuration (If Needed)

If you're using an AppShellConfigurator in your project, make sure it doesn't add any extra content to the page. Override the configurePage method to clear all default header tags:

@Route("")
public class EmptyHtmlView extends Html implements AppShellConfigurator {

    public EmptyHtmlView() {
        super("<html><head></head><body></body></html>");
        Page page = Page.getCurrent();
        page.getStyles().clear();
        page.getJavaScript().clear();
        page.getHead().getChildren().removeIf(child -> true);
    }

    @Override
    public void configurePage(AppShellSettings settings) {
        // Remove all auto-added meta tags, links, and scripts from the head
        settings.getHeaderTags().clear();
    }
}

Verify the Output

Once you run the app, navigating to the root route should serve exactly the HTML you want:

<html><head><!-- empty --></head><body><!-- empty --></body></html>

内容的提问来源于stack exchange,提问作者Naranmandakh Tsogoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:03:01