Java 索引页的Spring resourceHandler模式失败
我试图在SpringMVC应用程序中将ResourceHandler映射到resourceLocations,但不知何故,我无法使/*和index.html之间的映射正常工作Java 索引页的Spring resourceHandler模式失败,java,spring,spring-mvc,Java,Spring,Spring Mvc,我试图在SpringMVC应用程序中将ResourceHandler映射到resourceLocations,但不知何故,我无法使/*和index.html之间的映射正常工作 @Configuration @EnableWebMvc @EnableTransactionManagement(proxyTargetClass = true) @EnableScheduling @ComponentScan(basePackages = {"mySpringApp.web.controller"})
@Configuration
@EnableWebMvc
@EnableTransactionManagement(proxyTargetClass = true)
@EnableScheduling
@ComponentScan(basePackages = {"mySpringApp.web.controller"})
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/*", "/", "").addResourceLocations("classpath:/static/index.html");
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/static/assets/");
}
...
我提出的请求返回以下内容:
- -404
- -200(返回不在其上的索引文件/*)
- -200(返回其中一个文件 位于“我的资产”文件夹中)
@RequestMapping(“”
或@RequestMapping(“/”
)的控制器,而您没有
registry.addResourceHandler("/*", "/", "").addResourceLocations("classpath:/static/index.html");
不提供索引文件,因为:您只是没有指定希望spring提供什么资源 可能的解决方案
- 编写一个控制器
@RequestMapping(value = "/") public String index() { return "redirect:index.html"; }
- 重构
addResourceHandlers()
请注意,
addResourceHandler
正在添加一个“模式”,而addResourceLocation
正在告诉spring在哪里/物理上准确地找到请求的资源。aaaaah,所以我试图服务于一个位置而不是资源!有道理!非常感谢。
registry.addResourceHandler("*.html").addResourceLocations("/static/");