Java 如何从 tomcat 提供静态内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2523262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:48:26  来源:igfitidea点击:

How to serve static content from tomcat

javatomcat

提问by

I have a directory containing a number of static file (*.png, *.css, etc).
I thought (mistakenly perhaps) that just creating a directory in my application's WEB-INF file would suffice and I would be able to access the files by just referring to them by name:
Ex:

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

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

My directory structure is as follows:

我的目录结构如下:

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

My web.xml is as follows

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

But....
This doesn't work I just get a 404 - file not found when attempting to access the resources contained in the "static" directory. Is there something I'm missing here?

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

采纳答案by Thilo

Don't put them into WEB-INF, that folder is for the stuff that you do notwant to have directly served.

不要把它们放到WEB-INF,该文件夹是东西,你希望有直接投放。

Put it next to WEB-INF

把它放在 WEB-INF 旁边

+- WEB-INF
+- static

回答by Chandra Sekar

The files should not be inside WEB-INF. They must be placed directly inside your web-applications directory or WAR file.

这些文件不应位于 WEB-INF 中。它们必须直接放置在您的 web-applications 目录或 WAR 文件中。

Look at my answer herefor including the context path before your static resources.

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

回答by Robby Pond

WEB-INF is a protected directory. They must be placed in or in a subdirectory in the top level web app directory.

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