内容简介:springboot默认配置基本上可以满足我们的日常需要所以此时就需要自定义配置springboot的项目静态文件映射映射 /** 到
项目中需要将图片放在磁盘上,不能将图片放在webapp下面!
springboot默认配置基本上可以满足我们的日常需要
所以此时就需要自定义配置springboot的项目静态文件映射
springboot默认的配置规则
映射 /** 到
classpath:/static classpath:/public classpath:/resources classpath:/META-INF/resources
到本地文件路径也就是 resource/static/ 下面
访问时可以:
localhost:8080/+资源路径+资源名
例如我的项目结构!
此时我访问的静态资源为:
localhost:8080/js/jquery.min.js
如果配置 jquery.min.js 直接在static下面 访问则是
localhost:8080/jquery.min.js
但现在需要自定义映射规则:
有两种方法一种是基于配置文件,另一种是基于代码层面配置。
1 基于配置文件
#配置内部访问地址和外部图片访问地址 /myimgs/** spring.mvc.static-path-pattern=/** spring.resources.static-locations=file:C:/Users/tizzy/Desktop/img/,classpath:/static/
映射 /** 到 本地磁盘路径下存放的图片,和tomcat中的图片路径
访问路径则是
localhost:8080/jquery.min.js localhost:8080/ 图片名
2 基于代码层面配置
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceHandler是指你想在url请求的路径
//addResourceLocations是图片存放的真实路径
registry.addResourceHandler("/**").addResourceLocations("file:C:/Users/tizzy/Desktop/img/").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Beginning Apache Struts
Arnold Doray / Apress / 2006-02-20 / USD 44.99
Beginning Apache Struts will provide you a working knowledge of Apache Struts 1.2. This book is ideal for you Java programmers who have some JSP familiarity, but little or no prior experience with Ser......一起来看看 《Beginning Apache Struts》 这本书的介绍吧!