发布时间:2025-01-29 12:01
/static
/public
/resources
/META-INF/resources
动态资源目录:/templates
http://localhost:8080/a.jpg
http://localhost:8080/b.jpg
http://localhost:8080/c.png
http://localhost:8080/d.jpg
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//自定义路径mypic, addResourceLocations指定访问资源所在目录
registry.addResourceHandler(\"/mypic/**\").addResourceLocations(\"file:C:\\\\Users\\\\Administrator\\\\Desktop\\\\images1\\\\\");
//自定义路径webjars访问,addResourceLocations映射该路径下的资源,resourceChain资源链
// registry.addResourceHandler(\"/webjars/**\").addResourceLocations(\"classpath:/META-INF/resources/webjars/\").resourceChain(true);
}
}
路径:http://localhost:8080/mypic/huangshanpic.webp
web.pic-path=C:/Users/Administrator/Desktop/images1/
spring.mvc.static-path-pattern=/mypic/**
spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/public/,classpath:/static/,file:${web.pic-path}
web.pic-path:访问路径
spring.mvc.static-path-pattern:采用全部映射到mypic路径的方式
spring.web.resources.static-locations:配置允许访问的静态资源目录
http://localhost:8080/mypic/a.jpg
http://localhost:8080/mypic/b.jpg
http://localhost:8080/mypic/c.png
http://localhost:8080/mypic/d.jpg
http://localhost:8080/mypic/web.pic-path配置本地路径下的图片名称
创建一个新的web工程,只存放静态资源
1).pom.xml
WWebjarsdemo
1.0
${project.basedir}/src/main/resources
${project.build.outputDirectory}/META-INF/resources/webjars
2).静态资源目录结构
3). package点击打包
4).install到本地仓库
1).pom.xml
com.openlab
WWebjarsdemo
1.0
org.webjars
webjars-locator-core
0.35
未引入webjars-locator-core的jar包:http://localhost:8080/webjars/example/版本号/huangshan.webp
引入webjars-locator-core的jar包:
http://localhost:8080/webjars/example/huangshan.webp
注意:如果主程序和引入打包的jar包静态资源下具备相同的目录结构,如:META-INF\\resources\\webjars\\example\\0.0.1\\**,可能会出现路径访问失败的情况。
解决方法:clean主程序项目,重新运行。