【发布时间】: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