springboot访问静态资源如webapp下的js、css等

您所在的位置:网站首页 webapp目录下存放的前端资源 springboot访问静态资源如webapp下的js、css等

springboot访问静态资源如webapp下的js、css等

2024-02-20 08:23| 来源: 网络整理| 查看: 265

springboot访问静态资源如webapp下的js、css等

springboot对静态资源的默认扫描路径是:

classpath:/static classpath:/public classpath:/resources classpath:/META-INF/resources

首先,springboot默认建立是没有webapp文件夹的,需要自己创建:

在这里插入图片描述

主要是配置两个路径,webapp放在和java,resource统一目录下,如下:

在这里插入图片描述

如何访问到webapp下的这些css、img、js中的静态资源呢?

1、pom.xml src/main/webapp META-INF/resources **/**

这样,springboot才会将对应资源编译到target目录中,如下图:

在这里插入图片描述

2、配置

写一个配置类:

package com.tmall.tmallspringboot.config; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @EnableWebMvc @Configuration public class CORSConfiguration implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/"); } }

17行为将"/**" 映射到target中的"classpath:/META-INF/resources/"。

注意"classpath:/META-INF/resources/"中最后的/不能丢

综上,就可以找到了!

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3