[英]Java web.xml location for embedded jetty
I'm trying to understand the way we should configure the web application. 我试图了解我们应该如何配置Web应用程序。 Now i have a simple gradle project with embedded jetty 现在我有一个带有嵌入式码头的简单gradle项目
Dependencies: 依存关系:
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'
}
Application main: 应用主体:
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();
}
}
In web.xml I defined only ServletContextListener implementation to find if it was catched with application. 在web.xml中,我仅定义ServletContextListener实现来查找是否被应用程序捕获。
My problem is: webAppContext.setDescriptor("src/main/resources/WEB-INF/web.xml")
Jetty can find web.xml only with this weird location path. 我的问题是: webAppContext.setDescriptor("src/main/resources/WEB-INF/web.xml")
Jetty只能使用此奇怪的位置路径找到web.xml。
Why do I need to target it from project folder? 为什么需要从项目文件夹中定位它? If I run jar task with gradle the wouldn't be any src directory inside the jar. 如果我用gradle运行jar任务,则jar内将没有任何src目录。
Is exist a way to something like: App.class.getResource("/WEB-INF/web.xml")
and load web.xml related to classpath? 是否存在一种类似方法: App.class.getResource("/WEB-INF/web.xml")
并加载与类路径相关的web.xml?
Seems it was some class loaders issue. 似乎是一些类加载器问题。
After some further searches I ended with next solution: 经过进一步的搜索后,我得出了下一个解决方案:
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();
}
}
An the layout: 布局:
Thanks github user arey for the examle examle 感谢GitHub的用户arey为examle examle
your web.xml should be in 您的web.xml应该在
src/main/webapp/WEB-INF
UPD: sorry, pressed submit before finalising the post. UPD:对不起,请在完成帖子之前按提交。
above works for me and then I can run the test like: 以上对我有用,然后我可以像这样运行测试:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.