将Web应用程序上下文添加到Jetty
我想创建在Jetty下运行的简单的Spring MVC“Hello world”应用程序(这是应用程序的一部分)。
我的申请结构是:
|-WEB-INF | |-view | |-layout | |-index.jsp | |-web.xml | |-jetty.xml |-application-context.xml
我尝试创建Jetty服务器并基于web.xml文件添加Web应用程序上下文:
Resource jettyConfig = Resource.newSystemResource("jetty.xml"); XmlConfiguration configuration = new XmlConfiguration(jettyConfig.getInputStream()); Server server = (Server)configuration.configure(); WebAppContext wac = new WebAppContext(); wac.setDescriptor("WEB-INF/web.xml"); wac.setContextPath("/"); wac.setParentLoaderPriority(true); server.setHandler(wac); server.start();
服务器启动正常,但没有我的上下文:没有关于弹簧启动日志的信息,弹簧mvc控制器不可用。 有什么想法我做错了吗?
jetty.xml的内容:
9080 .
WEB-INF / web.xml的内容:
Hello world development true mvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation application-context.xml 1 mvc /* springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* org.springframework.web.context.ContextLoaderListener 404 /error/404.jsp 500 /error/500.jsp
如果您使用爆炸的war目录运行,请尝试显式设置资源基本属性:
context.setResourceBase("/path-to-your-project/WebContent"); context.setDescriptor("/path-to-your-project/WebContent/WEB-INF/web.xml");
或者如果您正在部署战争本身,您可以使用:
context.setWar("/path-to-your-project/WebContent");
以下是显示嵌入式Jetty示例的文档。
在您的应用环境中:
Resource jettyConfig = Resource.newSystemResource("jetty.xml"); XmlConfiguration configuration = new XmlConfiguration(jettyConfig.getInputStream()); Server server = (Server)configuration.configure(); WebAppContext wac = new WebAppContext(); wac.setResourceBase("."); wac.setDescriptor("WEB-INF/web.xml"); wac.setContextPath("/"); wac.setParentLoaderPriority(true); server.setHandler(wac); server.start();
这假设您运行服务器的基本路径与Web内容的路径相同。
application-context.xml文件应放在WEB-INF目录中。