繁体   English   中英

Java:具有Vaadin 11框架的Jetty服务器

[英]Java: Jetty server with Vaadin 11 framework

我的目标是配置Jetty服务器以与Vaadin 11框架一起使用。
我正在尝试以下操作:

public static void main(final String[] args) {
    final Server server = new Server();
    final ServerConnector httpConnector = new ServerConnector(server);
    httpConnector.setPort(8080);
    final ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    contextHandler.setContextPath("/");
    final ServletHolder servletHolder = new ServletHolder(new VaadinServlet());
    contextHandler.addServlet(servletHolder, "/*");
    final WebAppContext context = new WebAppContext();
    context.setServer(server);
    context.setContextPath("/");
    context.setClassLoader(Thread.currentThread().getContextClassLoader());
    server.addConnector(httpConnector);
    server.setHandler(contextHandler);
    server.setHandler(context);

    try {
        server.start();
    } catch (final Exception e) {
        e.printStackTrace();
    }
}


@Route("/")
public class MainView extends Div {
    public MainView() {
        System.out.println("main");
        setText("aha bye");
    }
}

但是从不调用MainView。 我如何告诉Jetty将请求转发给Vaadin?

在这里回答我自己的问题。

但这仍然无法如您所愿。 为什么? 因为您在同一个contextPath /上有两个不同的上下文( contextHandlercontext )。

但是同样,这似乎是倒退,因为您的WebAppContext没有资源库或没有声明战争,因此它在示例代码中没有为您做任何事情。

Joakim Erdfelt绝对正确。 我搞砸了的代码有两个背景我要么必须设置一个资源库或战争文件。 非常感谢您指出解决方案的方法!

还要感谢LeifÅstrand来提示使用@Route注释查找所有类的提示!

我将服务器代码与“客户端”代码分开,并为码头服务器构建了war文件。

我用作工作解决方案的方法如下:

final WebAppContext context = new WebAppContext();
context.setServer(httpServer);
context.setContextPath("/");
context.addServlet(new ServletHolder(new TestServlet()), "/*");
context.setWar(warFile.getAbsolutePath());
context.setExtractWAR(true);
context.setClassLoader(Thread.currentThread().getContextClassLoader());
context.setInitParameter("ui", MainUI.class.getCanonicalName());
httpServer.setHandler(context);

Testservlet (扩展VaadinServlet ),我有以下两种方法:

@SuppressWarnings("unchecked")
private void findRouteClasses() {
    final Set<Class<?>> routeClasses = ReflectionUtils.findClassesWithAnnotation(Route.class,
    "com.example.myproject");

    this.routeClasses = new HashSet<>();

    for (final Class<?> clazz : routeClasses)
        this.routeClasses.add((Class<? extends Component>) clazz);
}

@Override
protected void servletInitialized() throws ServletException {
    final ServletContext context = getServletContext();
    final Object routeRegistryObject = context.getAttribute(RouteRegistry.class.getName());

    if (routeRegistryObject == null)
        throw new ServletException("routeRegistryObject is null");

    if ((routeRegistryObject instanceof RouteRegistry) == false)
        throw new ServletException("routeRegistryObject is not of type RouteRegistry");

    final RouteRegistry routeRegistry = (RouteRegistry) routeRegistryObject;
    findRouteClasses();

    try {
        routeRegistry.setNavigationTargets(routeClasses);
    } catch (final InvalidRouteConfigurationException e) {
        throw new ServletException(e);
    }

    super.servletInitialized();
}

com.vaadin.flow.router.Route所有带有com.vaadin.flow.router.Route批注的类进行com.vaadin.flow.router.Route ,然后将其设置为导航目标。

server.setHandler(contextHandler);
server.setHandler(context);

您用该代码的context替换了contextHandler

试试这个代替...

HandlerList handlers = new HandlerList();
handlers.addHandler(contextHandler);
handlers.addHandler(context);
handlers.addHandler(new DefaultHandler()); // to report errors if nothing above matches
server.setHandler(handlers);    

但这仍然无法如您所愿。 为什么? 因为您在同一个contextPath /上有两个不同的上下文( contextHandlercontext )。

将使用HandlerList的第一个上下文,而永远不会调用下一个上下文。 为什么? 因为一旦输入上下文,就不会退出它(该上下文必须提供响应,甚至是错误)。

您可以修改WebAppContext使其包含VaadinServlet (完全消除对ServletContextHandler的需要)

例如:

final WebAppContext context = new WebAppContext();
final ServletHolder servletHolder = new ServletHolder(new VaadinServlet());
context.addServlet(servletHolder, "/*");

但是同样,这似乎是倒退,因为您的WebAppContext没有资源库或没有声明战争,因此它在示例代码中没有为您做任何事情。

如果是我,并且我使用的是 ,那么我将避免完全使用WebAppContext而仅坚持使用ServletContextHandler

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM