多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**1. 默认首页** SpringBoot 默认将首页放在下面的位置,模板名称默认`index.html`,这时访问 http://localhost:8080 ,或 http://localhost:8080/ ,或 http://localhost:8080/index.html 都可以访问到`index.html`。 ```html resources/META-INF/resources/index.html resources/public/index.html resources/resources/index.html resources/statics/index.html ``` 如果你在上面的这些目录中放置了`index.html`文件,则在`resources/templates/index.html`中的`index.html`将不再生效。 <br/> 如果非要将首页放置在`resources/templates/index.html`也可以,可以采用如下2中方式之一来改变默认的首页。 **2. 改变默认首页位置** 方式一:在controller层代码改变。 ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping(value = {"/index", "/"}) public String index() { return "index"; } } ``` 方式二:重写方法 addViewControllers 来改变。 ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CustomMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/index").setViewName("index"); } ```