[英]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
/
上有两个不同的上下文(contextHandler
和context
)。但是同样,这似乎是倒退,因为您的
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 /
上有两个不同的上下文( contextHandler
和context
)。
将使用HandlerList
的第一个上下文,而永远不会调用下一个上下文。 为什么? 因为一旦输入上下文,就不会退出它(该上下文必须提供响应,甚至是错误)。
您可以修改WebAppContext
使其包含VaadinServlet
(完全消除对ServletContextHandler
的需要)
例如:
final WebAppContext context = new WebAppContext();
final ServletHolder servletHolder = new ServletHolder(new VaadinServlet());
context.addServlet(servletHolder, "/*");
但是同样,这似乎是倒退,因为您的WebAppContext
没有资源库或没有声明战争,因此它在示例代码中没有为您做任何事情。
如果是我,并且我使用的是Embedded-jetty ,那么我将避免完全使用WebAppContext
而仅坚持使用ServletContextHandler
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.