Getting Started with Spring Boot - Basics (4) - Static Resources

Static resources include: HTML, CSS, JS, images, videos, PDF/Office and other files that do not require server-side processing.

(1) The file location is
the Maven project, and the static files are placed under src/main/webapp/.
quote
Project Root
└─src └─
    main └─
        webapp # Web application root directory
            └─ static └─
                css
                    └─ app.css


After packaging and publishing to Tomcat, the file path is:
quote
<TOMCAT_HOME>/webapps/spring-boot-sample/static/css/app.css


Access URL:
http://localhost:8080/spring-boot-sample/static/css/app.css

(2) Tomcat's request response
Tomcat responds to all request processing through org.apache.catalina.servlets.DefaultServlet.

<TOMCAT_HOME>\conf\web.xml.
    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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


After Servlet 3.0+, the jar file META-INF/resources in the war is also the root directory of the Web application.
quote
spring-boot-sample.war └─
WEB-INF └─
    lib
        └─ foo.jar └─
            META-INF
                └─ resources # Web application root directory
                    └─ static └─
                        css
                            └─ foo.css


Access URL:
http://localhost:8080/spring-boot-sample/static/css/foo.css

(2) Spring MVC request response
Usually Spring project distributes all requests to DispatcherServlet, so that Tomcat will not be called DefaultServlet, static files will not be accessible.

src/main/webapp/WEB-INF/web.xml
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


If you continue to use DefaultServlet to respond to static files, you need to open the request Forward to DefaultServlet.
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}


Or use Spring MVC's own static file processing
org.springframework.web.servlet.resource.ResourceHttpRequestHandler
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(604800);
    }
}


(3) SpringBoot's request response
SpringBoot has automatically identified "/static" "/public" "/resources" "/META-INF/resources" as resource folders.
quote
Project Root
└─src
    └─ main
        └─ resources
            ├─ static
            |   └─ css
            |       └─ a.css
            ├─ public
            |   └─ css
            |       └─ b.css
            ├─ resources
            |   └─ css
            |       └─ b.css
            └─ META-INF
                └─ resources
                    └─ css
                        └─ d.css

访问URL:
http://localhost:8080/css/a.css
http://localhost:8080/css/b.css
http://localhost:8080/css/c.css
http://localhost:8080/css/d.css


详细配置:
src/main/resources/application.properties
quote
spring.resources.static-locations=classpath:/static/
spring.resources.cache-period=604800
spring.resources.chain.gzipped=true
spring.resources.chain.cache=false
spring.resources.add-mappings=false


Instructions:
quote
──resources
    ├─static
    │  ├─css
    │  │      index.css
    │  └─js
    │          index.js
    └─templates
            index.ftl

<link rel="stylesheet" type="text/css" href="/css/index.css">
<script type="text/javascript" src="/js/index.js"></script>


Version management:
Since the browser will cache the static files, all the static files modified on the server side will not be reloaded by the browser immediately. Usually, a mark (timestamp or version number) will be added after the URL of the resource file. At the same time, modify the flag at the same time, because the URL is different, the browser will reload the resource file.
<script type="text/javascript" src="/js/sample.js?201702091030"></script>
<script type="text/javascript" src="/js/sample.js?v=1.0.1"></script>


a - resource name MD5 mode
src/main/resources/application.properties
quote
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**


b - version number method
src/main/resources/application.properties
quote
spring.resources.chain.strategy.fixed.enabled=true
spring.resources.chain.strategy.fixed.paths=/js/**,/v1.0.0/**
spring.resources.chain.strategy.fixed.version=v1.0.0

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326847735&siteId=291194637