【问题标题】:Configuring static content for a webapp with Jetty 9使用 Jetty 9 为 webapp 配置静态内容
【发布时间】:2014-05-01 11:48:44
【问题描述】:

谁能告诉我如何使用 Jetty 9 为 web 应用程序配置静态内容。我一直在尝试这样做,但我没有任何运气。所有对静态内容的请求(例如 css/* 或 img/*)都直接转到我的 / 的 servlet 处理程序。

root
    |-war
        |-css
        |-img
        |-js
        |-WEB-INF
                |-web.xml

我的 web.xml 看起来像:

<webapp>
    ...
    ...
    <servlet-mapping>
        <servlet-name>App</servlet-name>
        <url-pattern>/app</url-pattern>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- SOMETHING HERE FOR STATIC CONTENT ?? -->

</webapp>

我只是不明白如何处理码头文档中的这个例子:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/scratch</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="resourceBase">/home/jesse/scratch</Set>
      <Set name="directoriesListed">true</Set>
    </New>
  </Set>
</Configure>

我尝试过使用它并将文件放在不同的位置,但我无法让它工作。我的 servlet 处理得很好,但是我的页面没有样式、图像或 js,因为它们找不到内容。

【问题讨论】:

  • 正如 Jetty 文档中所说,您应该在“${jetty.home}/webapps”@eclipse.org/jetty/documentation/current/… 下创建 XML 文件,但这会引起很多问题,因为如果您需要在项目上下文(覆盖默认值)或在 Jenkins 等平台上使用它...

标签: java jetty


【解决方案1】:

我最终只是在我的 web.xml 中添加了这个:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
</servlet-mapping>

default 似乎只是根据文件扩展名以适当的 mime 类型交还任何内容,所以这很好。

【讨论】:

  • 你把文件放在项目的什么地方?
  • 在我的 Maven 项目中,我没有war文件夹。会不会是src/main/staticsrc/main/webapp 的“兄弟”?
  • 我以前从未使用过 Maven,但只是快速浏览了一些 Maven 文档,${basedir}/src/main/webapp/static/... 会是吗?
【解决方案2】:

我通过 Maven 插件使用 Jetty,我发现了这个: http://jamesphilipps.wordpress.com/2012/06/13/serving-static-content-with-the-maven-jetty-plugin/

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>7.1.0.RC0</version>
  <configuration>
    <contextHandlers>
      <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
        <contextPath>/static</contextPath>
        <resourceBase>src/main/webapp/static</resourceBase>
      </contextHandler>
    </contextHandlers>
  </configuration>
</plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多