如何从 tomcat 提供静态内容

问题描述 投票:0回答:4

我有一个目录,其中包含许多静态文件(*.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 - 文件未找到。我在这里缺少什么吗?

java tomcat
4个回答
61
投票

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

放在WEB-INF旁边

+- WEB-INF
+- static

5
投票

文件不应位于 WEB-INF 内。 它们必须直接放置在您的 Web 应用程序目录或 WAR 文件中。

查看我的答案,在静态资源之前包含上下文路径。


3
投票

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


1
投票

URL 路由和文件系统中的路径之间的映射必须在

server.xml
中定义(在
conf
目录或 Tomcat 服务器中)。

<Host>
文件中 Web 应用程序的
server.xml
元素中,添加如下内容:

<Context docBase="[filesystem path]" path="[webapp route]" />

例如:

<Host appBase="webapps"
    autoDeploy="false" name="localhost" unpackWARs="true"
    xmlNamespaceAware="false" xmlValidation="false">
    ...
    <Context docBase="/home/stuff" path="/static" />
</Host>

然后您可以通过 URL 访问您的目录

http://<your domain>/static

请注意,您的目录可以位于文件系统中您想要的任何位置,这通常很方便。

您可以在此处找到更多信息。这就是我得到这个例子的地方。

© www.soinside.com 2019 - 2024. All rights reserved.