简体   繁体   中英

Configuring static content for a webapp with Jetty 9

Could someone please tell me how to configure static content for a webapp with Jetty 9. I've been trying to do this for a while and I'm not having any luck. All requests for static content (eg css/* or img/*) just go straight to my servlet handler for the /.

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

My web.xml looks like:

<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>

I just don't understand what to do with this example from the jetty docs:

<?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>

I've tried playing around with it and putting the file in different places but I can't get it to work. My servlets are handled fine, but my pages have no styles, images or js because they can't find the content.

I ultimately just slapped this in my web.xml:

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

The default just seems to hand back whatever is there with an appropriate mime type based on the filename extension, so that's fine.

I am using Jetty through Maven Plugins, and I have found this: 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM