1

当我尝试在 Vaadin 14 中加载我的应用程序时,我看到一个像这样的空白屏幕......

在此处输入图像描述

看来 servlet 容器实际上正在运行,但它不能托管一些静态资源......

当我在 Chrome 检查器中查看网络选项卡时,我发现它找不到一些文件,例如

http://localhost:8080/VAADIN/static/client/client-122CE29AC0B9685B4DC485343E774096.cache.js

在此处输入图像描述

我不确定为什么会这样。有任何想法吗?

更新:

看来我必须手动确保我的所有 .jar 文件都作为资源添加到码头,以便可以提供静态内容。我试过这样做,它在我的笔记本电脑上运行良好,但在服务器上却不行......

public class App {


public static void main(String[] args) throws Exception {
    final var server = new Server(8080);

    // Specifies the order in which the configurations are scanned.
    Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
    classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");

    // Creation of a temporal directory.
    File tempDir = new File(System.getProperty("java.io.tmpdir"), "JettyTest");
    if (tempDir.exists()) {
        if (!tempDir.isDirectory()) {
            throw new RuntimeException("Not a directory: " + tempDir);
        }
    } else if (!tempDir.mkdirs()) {
        throw new RuntimeException("Could not make: " + tempDir);
    }

    WebAppContext context = new WebAppContext();
    context.setInitParameter("productionMode", "false");
    // Context path of the application.
    context.setContextPath("");
    // Exploded war or not.
    context.setExtractWAR(false);
    context.setTempDirectory(tempDir);

    // It pulls the respective config from the VaadinServlet.
    context.addServlet(GuiceVaadinServlet.class, "/*").setInitOrder(1);

    context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*");

    context.setParentLoaderPriority(true);
    server.setHandler(context);

    // This add jars to the jetty classpath in a certain syntax and the pattern makes sure to load all of them.
    final var classpathEntries = ClassPathHelper.getAllClassPathEntries();
    final var ideMode = classpathEntries.size() > 1;
    final var resourceList = new ArrayList<Resource>();
    final var jarFiles = new ArrayList<File>();

    if (ideMode) {
        System.out.println("Starting in IDE Mode");
        for (String entry : ClassPathHelper.getAllClassPathEntries()) {
            if (entry.endsWith(".jar")) {
                final var file = new File(entry);
                jarFiles.add(file);
            }
        }
    } else {
        final var baseInstallDir = System.getProperty("user.dir");
        System.out.println("Starting in Server WebJar Mode");
        final var libsDirectory = new File(baseInstallDir, "lib");
        System.out.println("Scanning for jars in " + libsDirectory.getPath());
        for (File file : Objects.requireNonNull(libsDirectory.listFiles())) {
            if (file.getPath().endsWith(".jar")) {
                jarFiles.add(file);
            }
        }
        final var sferionJar = new File(baseInstallDir, "sferion.jar");
        jarFiles.add(sferionJar);
        System.out.println("Found " + jarFiles.size() + " jar files");
    }

    for (File jarFile : jarFiles) {
        resourceList.add(Resource.newResource("jar:" + jarFile.toURI().toURL() + "!/"));
    }

    if (ideMode) {
        // It adds the web application resources. Styles, client-side components, ...
        //TODO: make this property dynamic somehow?
        resourceList.add(Resource.newResource("/usr/local/code/sferion/planglobal/src/main/webapp"));
    }

    // The base resource is where jetty serves its static content from.
    context.setBaseResource(new ResourceCollection(resourceList.toArray(new Resource[0])));

    server.start();
    server.join();
}

}

4

0 回答 0