java - 如何配置嵌入式 Jetty 来拾取网页片段?

标签 java xml spring configuration jetty

我的 Java 应用程序使用嵌入式 Jetty 9.2.2。我向包含 web_fragment.xml 文件的 pom.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>

最佳答案

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

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

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

需要理解的重要一点是,Web 片段是 Web 应用程序描述符的片段,这是 Web 应用程序的一个复杂功能,它是由 WebAppContext 跟踪的东西,它是由特定 WebAppContext 中定义的配置层列表配置的东西。

关于java - 如何配置嵌入式 Jetty 来拾取网页片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25804122/

相关文章:

Java:子节点值为空

java - 从 spring webservice 执行进程并监控?

java - 使用自定义 key 和 IV 在 Android 和 .Net 中进行 AES 128 加密

java - 如何在java中的过滤器中放置条件语句

java - Spring - 注入(inject) Autowiring 依赖项失败

xml - 为 TLS 向 XML 文档添加数字签名

Spring Boot 日志记录模式

java - 尝试使用 Java Spring AOP 抛出 "org.springframework.beans.factory.BeanCreationException"

java - findViewById 出现问题,用图表解释

java - 从选择查询中查找列名和元数据而不执行查询