【问题标题】:How to configure embedded Jetty to pick up web fragments?如何配置嵌入式 Jetty 以拾取 Web 片段?
【发布时间】:2014-09-12 08:42:01
【问题描述】:

我的 Java 应用使用嵌入式 Jetty 9.2.2。我向 pom.xml 添加了一个包含 web_fragment.xml 文件的库。但是 Jetty 并没有拾取该片段。当我启动应用程序时,我可以在日志中看到库已加载。但是,当从库向 servlet 发出请求时,应用程序会返回 404。 如何让它发挥作用?

在应用程序中有一个 Spring 配置文件 dispatcher-servlet.xml 并且该库包含在其中:

<import resource="classpath:/web.fragment.lib.spring.xml" />

没有 web.xml 文件,但应用程序包含带有映射的 spring.xml 文件。它使用 dispatcher-servlet.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
       default-lazy-init="false">

  <context:annotation-config/>
  <context:property-placeholder system-properties-mode="FALLBACK" location="classpath:config.properties"/>

  <bean name="WebServer" class="org.eclipse.jetty.server.Server" init-method="start">
    <property name="connectors">
      <list>
        <bean name="LocalSocket" class="org.eclipse.jetty.server.ServerConnector">
          <constructor-arg ref="WebServer"/>
          <property name="host" value="0.0.0.0"/>
          <property name="port" value="${jetty.port}"/>
        </bean>
      </list>
    </property>

    <property name="handler">
      <bean class="org.eclipse.jetty.server.handler.HandlerCollection">
        <property name="handlers">
          <list>
            <bean class="org.eclipse.jetty.servlet.ServletContextHandler">
              <property name="sessionHandler">
                <bean class="org.eclipse.jetty.server.session.SessionHandler"/>
              </property>
              <property name="contextPath" value="${context.path}"/>
              <property name="servletHandler">
                <bean class="org.eclipse.jetty.servlet.ServletHandler">
                  <property name="servlets">
                    <list>
                      <bean class="org.eclipse.jetty.servlet.ServletHolder">
                        <property name="name" value="dispatcherServlet"/>
                        <property name="servlet">
                          <bean class="org.springframework.web.servlet.DispatcherServlet"/>
                        </property>
                        <property name="initParameters">
                          <map>
                            <entry key="contextConfigLocation" value="**classpath:dispatcher-servlet.xml**"/>
                          </map>
                        </property>
                      </bean>
                    </list>
                  </property>
                  <property name="servletMappings">
                    <list>
                      <bean class="org.eclipse.jetty.servlet.ServletMapping">
                        <property name="pathSpecs">
                          <list>
                            <value>/</value>
                          </list>
                        </property>
                        <property name="servletName" value="dispatcherServlet"/>
                      </bean>
                    </list>
                  </property>
                </bean>
              </property>
            </bean>
          </list>
        </property>
      </bean>
    </property>
  </bean>
</beans>

【问题讨论】:

    标签: java xml spring configuration jetty


    【解决方案1】:

    Web Fragment auto configuration is a feature of the WebAppContext's Configuration layers.

    在您的示例中,您都没有使用。 您正在使用嵌入式 Jetty,并且正在手动构建 servlet 列表。

    您要么必须切换到通过 WebAppContext 构建应用程序,要么必须手动添加这些 Web 片段提供的功能。

    要理解的重要一点是,webfragment是webapp描述符的片段,这是一个webapp的复杂特性,它是由WebAppContext跟踪的东西,它是由Configuration列表配置的东西在该特定 WebAppContext 中定义的层。

    猜你喜欢
    • 2017-07-05
    • 1970-01-01
    • 2018-11-22
    • 2013-11-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多