SpringBoot 虛擬路徑配置
阿新 • • 發佈:2021-06-16
建立配置類
繼承WebMvcConfigurer
addResourceHandler() : 指訪問路徑,相當於Setting.xml裡的path屬性
addResourceLocations() :前面要有"file:" 後面接上對映的路徑,相當於Setting.xml裡面的docBase屬性
1 package com.my.affair.comm.configure; 2 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;5 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 6 7 @Configuration 8 public class WebConfig implements WebMvcConfigurer { 9 10 public static final String IMG_PATH = "D://images/"; 11 12 @Override 13 public void addResourceHandlers(ResourceHandlerRegistry registry) {14 /** 15 * @Description: 對檔案的路徑進行配置,建立一個虛擬路徑/img/** 16 * 這是圖片的物理路徑 "file:/+本地圖片的地址" 17 */ 18 registry.addResourceHandler("/img/**") 19 .addResourceLocations("file:" + IMG_PATH); 20 } 21 }
頁面引用
頁面上直接使用虛擬路徑 /img 加上檔名稱即可訪問
<img src="/img/picName.jpg"/>