【问题标题】:Java web.xml location for embedded jetty嵌入式码头的 Java web.xml 位置
【发布时间】:2016-07-13 06:59:07
【问题描述】:

我正在尝试了解我们应该如何配置 Web 应用程序。 现在我有一个带有嵌入式码头的简单 gradle 项目

依赖关系:

dependencies {
    compile('org.eclipse.jetty:jetty-servlet:9.3.10.v20160621')
    compile('org.eclipse.jetty:jetty-webapp:9.3.10.v20160621')

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

应用程序主:

package test;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class App {
    public static void main(String[] args) throws Exception {

        System.out.println(">> Running");
        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setDescriptor("src/main/resources/WEB-INF/web.xml");
        webAppContext.setResourceBase("/");
        webAppContext.setContextPath("/");

        Server server = new Server(8080);
        server.setHandler(webAppContext);
        server.start();
        server.join();
    }
}

在 web.xml 中,我只定义了 ServletContextListener 实现来查找它是否被应用程序捕获。

我的问题是:webAppContext.setDescriptor("src/main/resources/WEB-INF/web.xml") Jetty 只能通过这个奇怪的位置路径找到 web.xml。

为什么我需要从项目文件夹中定位它? 如果我使用 gradle 运行 jar 任务,则 jar 内不会有任何 src 目录。

是否存在类似:App.class.getResource("/WEB-INF/web.xml") 并加载与类路径相关的 web.xml 的方法?

【问题讨论】:

    标签: java jetty web.xml project-structure


    【解决方案1】:

    似乎是一些类加载器问题。

    经过一些进一步的搜索,我以下一个解决方案结束:

    public class App {
        private static final String WEBAPP_RESOURCES_LOCATION = "webapp";
    
        public static void main(String[] args) throws Exception {
            System.out.println(">> Running");
    
            WebAppContext webAppContext = new WebAppContext();
            webAppContext.setContextPath("/");
    
            URL webAppDir = Thread.currentThread().getContextClassLoader().getResource(WEBAPP_RESOURCES_LOCATION);
            webAppContext.setResourceBase(webAppDir.toURI().toString());
    
            // if setDescriptor set null or don't used jetty looking for /WEB-INF/web.xml under resource base 
    //        webAppContext.setDescriptor(webAppDir.toURI().toString() + "web.xml");
    
            Server server = new Server(8080);
            server.setHandler(webAppContext);
            server.start();
            server.join();
        }
    }
    

    布局:

    感谢 github 用户 arey 提供的示例 examle

    【讨论】:

      【解决方案2】:

      你的 web.xml 应该在

      src/main/webapp/WEB-INF
      

      UPD:抱歉,在完成帖子之前按了提交。

      以上对我有用,然后我可以像这样运行测试:

      Server server; //jetty server
      
      private static Integer portNum = 9999;
      private static String ENDPOINT_URL = "http://localhost:" + portNum + "/appName/";
      
      @Before 
      public void startJetty() throws Exception{
          server = new Server(portNum);
          server.setStopAtShutdown(true);
          WebAppContext webAppContext = new WebAppContext();
          webAppContext.setContextPath("/appName");
          webAppContext.setResourceBase("src/main/webapp");       
          webAppContext.setClassLoader(getClass().getClassLoader());
          server.setHandler(webAppContext);
          server.start();
      }
      
      @After
      public void stopJetty(){
          try {
              server.stop();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      
      @Test
      public void serverNotNull(){
          assertNotNull("jetty must be initialised", server);
      }
      

      【讨论】:

        猜你喜欢
        • 2014-01-14
        • 1970-01-01
        • 1970-01-01
        • 2020-02-02
        • 2014-03-15
        • 2014-02-25
        • 2019-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多