Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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?

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>

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
254 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

Here are the docs showing embedded Jetty samples.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...