如何从tomcat提供静态内容

时间:2010-03-26 12:44:13

标签: java tomcat

我有一个包含许多静态文件的目录(* .png,* .css等) 我想(也许是错误的)只是在我的应用程序的WEB-INF文件中创建一个目录就足够了,我只需通过名称引用它们就能访问这些文件:
例如:

   <link rel="stylesheet" href="/static/styles.css" type="text/css">

我的目录结构如下:

+WEB-INF
   |
   +---static
       |
       +--styles.css
       +--header.png

我的web.xml如下

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>myapp</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:com/example/myapp/spring/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <listener>
        <listener-class>
            com.example.myapp.ContextListener
        </listener-class>
    </listener>

    <!--  
        There are three means to configure Wickets configuration mode and they are
        tested in the order given. 
        1) A system property: -Dwicket.configuration
        2) servlet specific <init-param>
        3) context specific <context-param>
        The value might be either "development" (reloading when templates change)
        or "deployment". If no configuration is found, "development" is the default.
    -->

    <filter>
        <filter-name>wicket.myapp</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationFactoryClassName</param-name>
            <param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>wicket.session</filter-name>
        <filter-class>org.apache.wicket.protocol.http.servlet.WicketSessionFilter</filter-class>
        <init-param>
            <param-name>filterName</param-name>
            <param-value>wicket.myapp</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>wicket.myapp</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>wicket.session</filter-name>
        <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>
</web-app>

但是,....
这不起作用我只是在尝试访问“静态”目录中包含的资源时得到404文件。这里有什么我想念的吗?

3 个答案:

答案 0 :(得分:48)

不要将它们放入WEB-INF,该文件夹用于想要直接提供的内容。

将它放在WEB-INF旁边

+- WEB-INF
+- static

答案 1 :(得分:2)

文件不应该在WEB-INF中。它们必须直接放在您的web应用程序目录或WAR文件中。

请查看我的answer here,了解静态资源之前的上下文路径。

答案 2 :(得分:2)

WEB-INF是受保护的目录。它们必须放在顶级Web应用程序目录中的子目录中。

相关问题