轉載:https://blog.csdn.net/weixin_42236404/article/details/90758737
由於Springboot使用的是內置的tomcat,因此我們需要創建如下一個配置類,繼承WebMvcConfigurer,重寫其方法
addResourceHandler() : 指訪問路徑,相當於Setting.xml里的path屬性
addResourceLocations() :前面要有"file:" 后面接上映射的路徑,相當於Setting.xml里面的docBase屬性
網上有一些是繼承 WebMvcConfigurerAdapter 這個已經過時了。官方建議WebMvcConfigurer
@Configuration public class WebMvcConfig implements WebMvcConfigurer{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/img/**").addResourceLocations("file:D:/upload/"); } }
顯示圖片:
//如果值不為空,則顯示文件圖片,反之顯示默認圖片 <img th:src="${house.photopath != null} ? |/img/${house.photopath}| :'../images/thumb_house.gif' " width="100" height="75"