【问题标题】:Vaadin Fusion index.html will result in offline-stubVaadin Fusion index.html 将导致offline-stub
【发布时间】:2021-12-01 15:36:17
【问题描述】:

我对 Jetty 和 Vaadin 很陌生。我尝试让 Jetty 运行托管一个最小的 Vaadin Fusion 示例。
但是,第一次访问我的空 index.html 时,我得到了 connection lost 横幅和 404,因为它试图重定向到 /offline-stub .html 我没有。

生产者视图:

在网络点击中,您可以看到 Vaadin 以某种方式启动了重定向:

我的 index.html 很简单:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Vaadin Grocery App</title>
    <style>
      body {
        margin: 0;
        width: 100vw;
        height: 100vh;
      }

      #outlet {
        height: 100%;
      }
    </style>
    <!-- index.ts is included here automatically (either by the dev server or during the build) -->
  </head>

  <body>
   <h1>hello</h1>
  </body>
</html>

还有一个 index.ts:

import { Router } from '@vaadin/router';
import { routes } from './routes';
import { appStore } from './stores/app-store';

export const router = new Router(document.querySelector('#outlet'));

router.setRoutes(routes);

window.addEventListener('vaadin-router-location-changed', (e) => {
  appStore.setLocation((e as CustomEvent).detail.location);
  const title = appStore.currentViewTitle;
  if (title) {
    document.title = title + ' | ' + appStore.applicationName;
  } else {
    document.title = appStore.applicationName;
  }
});

我这样启动 Jetty:

 public static void main(String[] args) throws URISyntaxException, IOException {

        Log.setLog(new StdErrLog());
        System.out.println("Server starting...");
       

        String extForm = webRoot + "/webapp";
        System.out.println(extForm);

        final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        final ResourceHandler resHandler = new ResourceHandler();
        resHandler.setResourceBase(extForm);
        final ContextHandler ctx = new ContextHandler("/");
        ctx.setHandler(resHandler);

        Server embeddedServer = new Server(8080);

        embeddedServer.insertHandler(context); // Rest/Servlets
        embeddedServer.insertHandler(resHandler); // HTML Resources
        try {
            embeddedServer.start();
            embeddedServer.join();
        } catch (Exception e) {
            System.err.println("Server error:\n" + e);
        }
        System.out.println("Server stopped");
    }

Webroot, extform - 值正确且文件位于磁盘上的指定位置:

【问题讨论】:

    标签: jetty vaadin vaadin-fusion hilla


    【解决方案1】:

    请注意,Vaadin Fusion 目前仅支持 Spring Boot 后端。因此,您不需要配置自己的服务器。启动 Spring Boot 应用程序就足够了。我建议在 https://start.vaadin.com 上创建一个项目启动器并以此为基础。

    @SpringBootApplication
    public class Application extends SpringBootServletInitializer implements AppShellConfigurator {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
    
    

    【讨论】:

    • 感谢您提供的信息。你能解释一下为什么需要 Spring Boot。 Spring Boot 可以配置为使用 jetty 作为底层 servlet 容器。 vaadin fusion 还需要 spring boot 带来什么
    • Fusion 使用 Spring Boot 来注册和管理端点。我们想从一个自以为是的技术堆栈开始,这样我们就可以更快地验证和构建产品。一旦我们处于良好状态,我们肯定会考虑支持 Quarkus 等替代技术。
    【解决方案2】:

    不要像那样把ResourceHandlerServletContextHandler 混在一起。

    完全删除ResourceHandler(它与ContextHandler相关联)

    使用资源库正确定义ServletContextHandler
    然后确保ServletContextHandler 具有DefaultServlet 设置。

    就是这样,这就是你所要做的。

    哦,不要在 url-pattern //* 上声明您的 REST 端点,因为这也会阻止静态资源服务。这些 url 模式基本上意味着您的 servlet 端点(您的 REST 库?)是唯一的,它负责 100% 的请求。

    你不混合这样的东西的原因是因为ServletContextHandlerResourceHandler都是终端(一旦输入,它们必须以HTTP响应响应)。

    如果您正确使用 ServletContext,则使用针对 url-pattern 的最佳匹配(请参阅 Servlet url-pattern 匹配规则,例如:最长匹配),根据您的客户端请求提供静态内容或您的 REST 端点.

    【讨论】:

    • 感谢您的回答。它没有解决我的问题,但它是关于设置的非常有用的信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 2021-06-14
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多