springmvc无法映射资源的解决

**

//记一次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
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值