Jetty9.4.8嵌入JavaLite ActiveWeb遇异常及FreeMarker模板问题求助
Hey, let's work through your ActiveWeb + Jetty 9.4.8 embedded app issues step by step. I’ve dealt with similar ServletContext and template resolution headaches before, so here’s what I’d suggest:
The root problem here is that FreeMarker can’t resolve templates because ActiveWeb isn’t getting a valid ServletContext from Jetty. Let’s fix that first:
Properly wire ActiveWebFilter into Jetty’s WebAppContext
ActiveWeb relies on itsActiveWebFilterto bootstrap the application context. You need to ensure this filter is added correctly to yourWebAppContext, and that Jetty initializes the ServletContext before the filter runs. Here’s a working code snippet to reference:Server server = new Server(8080); WebAppContext webAppContext = new WebAppContext(); // Critical: Point to your web resources root (where WEB-INF lives) webAppContext.setResourceBase("src/main/webapp"); webAppContext.setContextPath("/"); // Add ActiveWebFilter and configure your app's root package FilterHolder activeWebFilter = new FilterHolder(ActiveWebFilter.class); activeWebFilter.setInitParameter("root_package", "com.your.app.package"); // Replace with your actual package webAppContext.addFilter(activeWebFilter, "/*", EnumSet.allOf(DispatcherType.class)); // Add Jetty's DefaultServlet to serve static content (required for proper context setup) ServletHolder defaultServlet = new ServletHolder("default", DefaultServlet.class); webAppContext.addServlet(defaultServlet, "/"); server.setHandler(webAppContext); server.start();The
resourceBaseis non-negotiable here—it tells Jetty where to load web resources from, which is necessary to create a valid ServletContext.Verify your FreeMarker template location
By default, ActiveWeb looks for FreeMarker templates insrc/main/webapp/WEB-INF/views. Double-check your templates are in this directory, and that yourresourceBasepoints to the parentsrc/main/webappfolder. If you’ve customized the template path, add this init parameter to the ActiveWebFilter:activeWebFilter.setInitParameter("freemarker.templates.path", "/WEB-INF/custom-templates");
Since you mentioned you resolved initial exceptions, here’s a quick recap of common pitfalls to avoid for anyone else hitting this:
Stick with WebAppContext, not other Handler types
If you triedServletContextHandlerinitially, that’s likely why you ran into issues.WebAppContextprovides full Servlet 3.x support, which ActiveWeb needs for filter initialization and resource resolution.Eliminate Maven dependency conflicts
Make sure your pom.xml doesn’t mix Jetty versions (e.g., using 9.4.8 alongside newer Jetty artifacts) or miss core ActiveWeb dependencies. Here’s a minimal dependency set to use:<dependency> <groupId>org.javalite</groupId> <artifactId>activeweb</artifactId> <version>2.6</version> <!-- Use the latest version compatible with Jetty 9.4.x --> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.4.8.v20171121</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.8.v20171121</version> <scope>provided</scope> </dependency>All Jetty artifacts must be on the exact same version to prevent classpath clashes.
If you’re still having trouble, add some debug checks to validate the ServletContext:
- In your ActiveWeb controller, add this code:
This will tell you if the context is properly loaded and if your template directory is accessible via the ServletContext.ServletContext context = getServletContext(); if (context == null) { log.error("ServletContext is NULL!"); } else { log.info("ServletContext initialized. Resource base: " + context.getRealPath("/")); log.info("Template directory exists: " + new File(context.getRealPath("/WEB-INF/views")).exists()); }
内容的提问来源于stack exchange,提问作者mls_dev

