【问题标题】:Add web application context to Jetty将 Web 应用程序上下文添加到 Jetty
【发布时间】:2010-12-08 16:57:50
【问题描述】:

我想创建在 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();

服务器启动正常,但没有我的上下文:日志中没有关于 spring 启动的信息,spring mvc 控制器不可用。有人知道我做错了什么吗?

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的内容:

  <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>

【问题讨论】:

    标签: java spring-mvc


    【解决方案1】:

    如果您使用爆炸式战争目录运行,请尝试显式设置资源基础属性:

    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");
    

    这里是显示embedded Jetty samples 的文档。

    在您的应用上下文中:

    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 内容的路径相同。

    【讨论】:

    • 谢谢!设置 wac.setResourceBase(".") 对我没有帮助,但是当我更改“。”时到绝对路径 (new ClassPathResource(".").getURI().toString()) 路径我的问题解决了!详情请见stackoverflow.com/questions/1462953/…
    【解决方案2】:

    application-context.xml 文件应该放在 WEB-INF 目录中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-04
      • 2013-04-12
      • 2021-09-15
      • 2017-12-09
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多