Java Spring Boot:如何将我的应用程序根目录(“/”)映射到index.html? [英] Java Spring Boot: How to map my app root (“/”) to index.html?
问题描述
我是Java和Spring的新手。
如何将我的应用根 http:// localhost:8080 /
映射到静态 index.html
?
如果我导航到 http:// localhost:8080 / index.html
它的工作正常。
I'm new to Java and to Spring.
How can I map my app root http://localhost:8080/
to a static index.html
?
If I navigate to http://localhost:8080/index.html
its works fine.
我的应用程序结构是:
我的 config \WebConfig.java
如下所示:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}
我试图添加注册表.addResourceHandler(/)。addResourceLocations(/ index.html);
但它失败了。
I tried to add registry.addResourceHandler("/").addResourceLocations("/index.html");
but it fails.
推荐答案
如果您没有使用 @EnableWebMvc
注释,它可以开箱即用。当你这样做时,你可以在 WebMvcAutoConfiguration
中关闭Spring Boot为你做的所有事情。你可以删除那个注释,或者你可以添加回你关闭的视图控制器:
It would have worked out of the box if you hadn't used @EnableWebMvc
annotation. When you do that you switch off all the things that Spring Boot does for you in WebMvcAutoConfiguration
. You could remove that annotation, or you could add back the view controller that you switched off:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
这篇关于Java Spring Boot:如何将我的应用程序根目录(“/”)映射到index.html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!