Vaadin 10/11 and embedded Jetty

旧巷老猫 提交于 2019-12-21 20:10:05

问题


I developed middle-size application on Vaadin 8.5.1. Jetty embedded 9.4.8 was used as Servlet container for Vaadin servlet. In Java code i initialize Jetty instance, create Vaadin servlet and attach it to Jetty. In Maven i use 'vaadin-maven-plugin' which helps me make correct settings to folders, also packaging is 'jar'. The Spring (not Spring Boot) are used for application configuration purposes and IoC.

Now i want to migrate project to Vaadin 10/11. I was tried all Vaadin Starter Packs which generates output JAR. But didn't understand how can i modify those packs to remove Spring Boot and get a simple Maven project with Jetty embedded.

Already asked question in Vaadin forum: Vaadin 10 + Jetty embedded


回答1:


You have to configure the Jetty Server as follows:

public class Application {

    public static void main(String... args) throws Exception {
        new Application().run(8080, "/");
    }

    public void run(int port, String contextPath) throws Exception {
        URL webRootLocation = this.getClass().getResource("/META-INF/resources/");
        URI webRootUri = webRootLocation.toURI();

        WebAppContext context = new WebAppContext();
        context.setBaseResource(Resource.newResource(webRootUri));
        context.setContextPath(contextPath);
        context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*");
        context.setConfigurationDiscovered(true);
        context.setConfigurations(new Configuration[]{
                new AnnotationConfiguration(),
                new WebInfConfiguration(),
                new WebXmlConfiguration(),
                new MetaInfConfiguration(),
                new FragmentConfiguration(),
                new EnvConfiguration(),
                new PlusConfiguration(),
                new JettyWebXmlConfiguration()
        });
        context.getServletContext().setExtendedListenerTypes(true);
        context.addEventListener(new ServletContextListeners());

        Server server = new Server(port);
        server.setHandler(context);

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

}

Also, you need to use the maven-shade-plugin if you want to package the artifact as an uber-jar.

You can find an example for Vaadin 12+ at https://github.com/alejandro-du/embedded-jetty-demo



来源:https://stackoverflow.com/questions/52420045/vaadin-10-11-and-embedded-jetty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!