**
//记一次springmvc-tomcat无法映射WEB-INF(reources)下的静态资源的事件(IDEA开发工具)
上网查的各种奇怪的资料,均实现不了,卡了我3小时,最后还是上官网查出来了
**
SpringMvc官网
以下文档摘自官网
1.11.10. Static Resources()
WebFlux
This option provides a convenient way to serve static resources from a list of Resource-based locations.
In the next example, given a request that starts with /resources, the relative path is used to find and serve static resources relative to /public under the web application root or on the classpath under /static. The resources are served with a one-year future expiration to ensure maximum use of the browser cache and a reduction in HTTP requests made by the browser. The Last-Modified header is also evaluated and, if present, a 304 status code is returned.
这个选项提供了一个方便的方式去服务静态资源从一系列基于资源的位置。
在下一示例中,提供了一个请求以“/resources” 开头,这一相对路径被用于去寻找并且服务静态资源,且该静态资源相对于“/public”路径于web应用的根路径下,或者在“/static”下的类路径。这一资源被服务一个一年为期的持续时间去保证对于浏览器缓存的最大利用并且也使浏览器发出的Http请求能够减少。这个最后修改的Header也被判断并且,如果其存在,一个304状态码会被返回。
(简述:在resources下新建static目录,配置下面的java代码即可被访问该目录下静态资源,且有效期为1年)
The following listing shows how to do so with Java configuration:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public", "classpath:/static/")
.setCachePeriod(31556926);
}
}
//The following example shows how to achieve the same configuration in XML:
<mvc:resources mapping="/resources/**"
location="/public, classpath:/static/"
cache-period="31556926" />
图1
图2