Java 将 Web 应用程序上下文添加到 Jetty
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4390093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add web application context to Jetty
提问by mikhail
I want to create simple Spring MVC "Hello world" application that runs under Jetty (wich is a part of application).
我想创建在 Jetty 下运行的简单 Spring MVC“Hello world”应用程序(这是应用程序的一部分)。
Structure of my application is:
我的应用程序的结构是:
|-WEB-INF
| |-view
| |-layout
| |-index.jsp
| |-web.xml
|
|-jetty.xml
|-application-context.xml
I try to create Jetty server and add web application context based on web.xml file:
我尝试创建 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();
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?
服务器启动正常,但没有我的上下文:日志中没有关于 spring 启动的信息,spring mvc 控制器不可用。有人知道我做错了什么吗?
Content of jetty.xml:
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>
Content of WEB-INF/web.xml:
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>
采纳答案by Joel
If you are running with an exploded war directory try setting the resource base property explicitly:
如果您使用分解的 war 目录运行,请尝试显式设置资源库属性:
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:
或者,如果您要部署War本身,则可以使用:
context.setWar("/path-to-your-project/WebContent");
Here are the docs showing embedded Jetty samples.
以下是显示嵌入式 Jetty 示例的文档。
In the context of your app:
在您的应用程序上下文中:
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 内容的路径相同。
回答by user1092781
The application-context.xml file should be placed inside the WEB-INF directory.
application-context.xml 文件应该放在 WEB-INF 目录中。