直接上代碼:
public class StaticResourcesConfig extends WebMvcConfigurerAdapter { private static final Log log = LogFactory.getLog(StaticResourcesConfig.class); @Override /** * 設置默認首頁 */ public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("redirect:/index.html"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); super.addViewControllers(registry); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/path/**").addResourceLocations("file:/html/); } }
第二個方法可以添加靜態資源映射,
在linux下,如果通過http://[server ip]/path/index.html進行訪問,則可以訪問到磁盤路徑為 /html/index.html 的文件
在windows下則可以將代碼中的 /html/ 換成 [盤符]:/html/ 如 D:/html/
提醒一下:
如果用的是容器,不要忘記目錄映射