簡體   English   中英

使用Jetty 9為webapp配置靜態內容

[英]Configuring static content for a webapp with Jetty 9

有人可以請告訴我如何使用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,因為它們找不到內容。

我最終只是在我的web.xml中打了這個:

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

default似乎只是根據文件擴展名將適當的mime類型移交給那里,所以沒關系。

我通過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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM