Add Web Application Context to Jetty

I want to create a simple Spring MVC Hello World application that runs on Jetty (which is part of the application).

The structure of my application:

|-WEB-INF
| |-view
| |-layout
| |-index.jsp
| |-web.xml
|
|-jetty.xml
|-application-context.xml

I am trying to create a Jetty server and add a web application context based on the 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();

The server starts normally, but without my context: there is no information about Spring startup in the logs, Spring mvc controllers are not available. Does anyone have any ideas what I'm doing wrong?

The 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 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>
+3
source share
2 answers

If you are working with an exploded military directory, try setting the database property explicitly:

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

, , , , -.

+9

application-context.xml WEB-INF.

+1

Source: https://habr.com/ru/post/1779224/


All Articles