将Web应用程序上下文添加到Jetty [英] Add web application context to Jetty
问题描述
我想创建在Jetty下运行的简单的Spring MVCHello world应用程序(这是应用程序的一部分)。
I want to create simple Spring MVC "Hello world" application that runs under Jetty (wich is a part of application).
我的应用程序的结构是:
Structure of my application is:
|-WEB-INF
| |-view
| |-layout
| |-index.jsp
| |-web.xml
|
|-jetty.xml
|-application-context.xml
I尝试创建Jetty服务器并基于web.xml文件添加Web应用程序上下文:
I try to create Jetty server and add web application context based on web.xml file:
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();
服务器启动正常,但没有我的上下文:没有关于弹簧启动日志,spring mvc控制器的信息不可用。有什么想法我做错了吗?
Server starts fine, but without my context: no info about spring start up in logs, spring mvc controllers are not available. Have anybody ideas what I do wrong?
jetty.xml的内容:
Content of jetty.xml:
<Configure id="server" class="org.mortbay.jetty.Server">
<Call name="addConnector">
<Arg>
<New class="org.mortbay.jetty.nio.SelectChannelConnector">
<Set name="port">9080</Set>
</New>
</Arg>
</Call>
<Set name="handler">
<New class="org.mortbay.jetty.handler.HandlerList">
<Set name="handlers">
<Array type="org.mortbay.jetty.Handler">
<Item>
<New class="org.mortbay.jetty.handler.DefaultHandler" />
</Item>
<Item>
<New class="org.mortbay.jetty.handler.ResourceHandler">
<Set name="resourceBase">.</Set>
</New>
</Item>
</Array>
</Set>
</New>
</Set>
</Configure>
WEB-INF / web.xml的内容:
Content of WEB-INF/web.xml:
<web-app>
<display-name>Hello world</display-name>
<init-param>
<param-name>development</param-name>
<param-value>true</param-value>
</init-param>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
</web-app>
推荐答案
如果您正在运行爆炸的战争目录,请尝试设置显式的资源基本属性:
If you are running with an exploded war directory try setting the resource base property explicitly:
context.setResourceBase("/path-to-your-project/WebContent");
context.setDescriptor("/path-to-your-project/WebContent/WEB-INF/web.xml");
或者如果您正在部署战争本身,您可以使用:
or if you are deploying the war itself you can just use:
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();
这假设您运行服务器的基本路径与路径相同网络内容。
This assumes that your base path where you're running your server from is the same as the path to the web content.
这篇关于将Web应用程序上下文添加到Jetty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!