2

我的嵌入式 tomcat 类识别 web.xml 和 ServletContainerInitializers,但它不识别 web 片段。非常简单的代码。应该打开什么标志/配置来初始化 web 片段?

    public static void main( String[] args ){

    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.setLevel(Level.INFO);

    String webappDirLocation = "src/main/webapp/";
    Tomcat tomcat = new Tomcat();

    tomcat.getHost().setAutoDeploy(true);
    tomcat.getHost().setDeployOnStartup(true);
    tomcat.setPort(8080);


    StandardContext ctx = null;
    try {
        ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
    } catch (ServletException e) {
        e.printStackTrace();
    }


    System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

    File additionWebInfClasses = new File("target/classes");
    WebResourceRoot resources = new StandardRoot(ctx);
    resources.addPreResources(new DirResourceSet(resources,
                                "/WEB-INF/classes",
                                additionWebInfClasses.getAbsolutePath(),
                                "/"));

    ctx.setResources(resources);
    ctx.setAltDDName( webappDirLocation + "WEB-INF/web.xml");
    ServletContextListener scl = new AdminServletContextListener();
    ctx.addApplicationEventListener(scl);

    try {

        tomcat.start();
        tomcat.getServer().start();
    } catch (LifecycleException e) {
        e.printStackTrace();
    }

    tomcat.getServer().await();
}
4

0 回答 0