破解这个 . documentation说:
默认情况下,Spring Boot将从类路径中的/ static(或/ public或/ resources或/ META-INF / resources)目录或ServletContext的根目录中提供静态内容 .
最小例子:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
@EnableWebMvc
public class Main {
public static void main(final String[] args) {
SpringApplication.run(Main.class, args);
}
}
并在那里创建一个目录 /src/main/resources/public
放置静态资源 . 当我运行应用程序时,我只得到404.当我 remove @EnableWebMvc
资源按预期提供时 .
尝试(和失败)补救措施
/**
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/public/**").addResourceLocations("/public/");
}
在 application.properties
补充说:
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
这个问题
所以我的问题是:当我想使用 @EnableWebMvc
注释时,我需要配置什么来提供静态资源?
2 回答
在你提到的_1108566中,它说:
您应该尝试在配置中使用
@EnableWebMvc
而不是Spring启动应用程序 . 在这个_1108568中有一个例子 .在这些例子中:
Customizing the Provided Configuration
Conversion and Formatting
我希望这能帮到您 .
您应该使用ResourceHandlers配置
ViewResolver
之类的内容 . 检查this