【问题标题】:Tomcat war - serve css files?Tomcat 战争 - 提供 css 文件?
【发布时间】:2020-04-16 10:32:14
【问题描述】:

我需要与战争一起提供任意文件。在 webapps/$project 中放在哪里,在 web.xml 中配置?

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app
        version="4.0"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd
        "
>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <filter>
        <filter-name>httpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>

    <filter-mapping>
        <filter-name>httpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Spring MVC-->
    <servlet>
        <servlet-name>servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <!--End of Spring MVC-->

    <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
</web-app>

【问题讨论】:

    标签: java spring tomcat war


    【解决方案1】:

    要提供 html、css 等静态内容,请将文件放在您的 $tomcat_home/webapps/$project 目录下的任意目录下。

    $tomcat_home
      +--bin
      +--conf
      +--...
      +--webapps
         +--your-app (Ex: file-server)
           +-- your-static-content-directory (Ex: static)
              +--css
                 +--a.css
                 +--b.css
              +--html
                 +--index.html
                 +--contact.html
           +-- WEB-INF
    

    假设您的 Tomcat 在port 8080 上的locally 上运行,您可以通过http://localhost:8080/file-server/static/css/a.css 访问文件a.css

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 2018-11-03
      • 2012-11-27
      • 2019-09-26
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多