【发布时间】:2012-08-01 21:52:03
【问题描述】:
我有一个带有嵌入式码头服务器的应用程序,我正在像这样启动它(放置在 main() 中并使用 eclipse 启动):
Server server = new Server(port);
WebAppContext context = new WebAppContext();
context.setResourceBase("web/");
context.setDescriptor("web/WEB-INF/web.xml");
context.setConfigurations(new Configuration[]{
new AnnotationConfiguration(), new WebXmlConfiguration(),
new WebInfConfiguration(), new TagLibConfiguration(),
new PlusConfiguration(), new MetaInfConfiguration(),
new FragmentConfiguration(), new EnvConfiguration()});
context.setContextPath("/");
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();
server.join();
我的 web.xml 看起来像这样(暂时为空,我不确定是否可以将其完全删除):
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false"
version="3.0">
</web-app>
我有一个像这样设置的简单类:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns={"/test"})
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/html/index.html").forward(request,response);
}
}
当我在 web.xml 中使用传统的 servlet 映射时,我的应用程序运行良好。但是当我删除 web.xml 映射并使用注释时,我只得到 404。它看起来根本不像在扫描注释。控制台如下所示:
2012-08-01 17:40:37.021:INFO:oejs.Server:jetty-8.1.5.v20120716
2012-08-01 17:40:37.227:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2012-08-01 17:40:37.294:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/me/project/web/}
2012-08-01 17:40:37.547:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/me/project/web/}
2012-08-01 17:40:37.547:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/me/project/web/}
2012-08-01 17:40:37.547:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/me/project/web/}
2012-08-01 17:40:37.641:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
我已经从我的研究中检查了一些事情:
- servlet-3.0 jar 在类路径中
- web.xml 中的元数据完整设置为 false
- 我确保在 Web 应用上下文中包含 AnnotationConfiguration
我的想法已经用完了,正要恢复到旧的 web.xml,但是为什么我不能让它工作,这让我很生气。
【问题讨论】:
-
Joakim Erdfelt 的回答真的有效吗?
-
不适合我(Jetty 9.0)。与@user1569803 相同,并且所有依赖项都已到位。我正在研究替代路线。
-
不幸的是,我无法再访问出现此问题的项目,因此我无法验证我的情况的答案。如果一些老同事尝试解决方案并成功了,我会为他们标记答案。
标签: annotations jetty embedded-jetty