现在我总是通过application.properties
文件中的静态路径提供图像:
spring.resources.staticlocations=file:/Applications/MAMP/htdocs/reportMaker/template
spring.mvc.static-path-pattern=/resources/**
然后做
http://localhost:8080/resources/logo.png
我已经够到logo了
现在我的目标是从我的数据库中获取一个文件夹路径。
我试过这个方法:
@EnableWebMvc
@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {
@Autowired
ConfigurationRepository confRepo;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
myConfiguration conf = confRepo.findByConfKey("downloadPath");
String path = conf.getConfValue();
if(path !=null) {
registry.addResourceHandler("/resources/**").addResourceLocations(path);
}
}
但是我不能像以前那样够到logo。
路径变量为/Applications/MAMP/htdocs/reportMaker/template
。
路径变量为
/Applications/MAMP/htdocs/reportMaker/template
。
根据此文档https://www.baeldung.com/spring-mvc-static-resources,路径应该以file:/
我通过删除:@EnableWebMvc
并在我的路径末尾添加/
来解决
@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("file:/Applications/MAMP/htdocs/reportMaker/template/");
}
}