使用Jetty 9为webapp配置静态内容

时间:2014-05-01 11:48:44

标签: java jetty

有人可以告诉我如何使用Jetty 9为webapp配置静态内容。我一直试图这样做一段时间,我没有运气。所有对静态内容的请求(例如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>

我只是不明白如何处理这个来自jetty文档的例子:

<?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,因为它们找不到内容。

2 个答案:

答案 0 :(得分:1)

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

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

default似乎只是根据文件扩展名将适当的mime类型交给那里的所有内容,这样就可以了。

答案 1 :(得分:1)

我通过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>
相关问题