spring boot配置默认页面
使用index.html作为首页面
-
问题描述:
index.html在webapp目录下,网站访问的时候需要http://localhost:8080/index.html,现在不想要index.html出现,只想要http://localhost:8080就可以访问网站 -
解决办法一:使用
Controller进行转发
```java package controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
/**
-
@author zhangzhen */ @Controller public class HelloController {
@RequestMapping(“/”) public String hello() { return “forward:index.html”; } } ``` notice:
@Controller用于页面等的转发,@RestController用于接口的转发,使用@Controller定位到页面,使用@RestController返回的是文本forward:index.html -
解决方法二:自定义MVC配置,
重写addViewControllers方法进行转发
```java package controller;
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
-
@author zhangzhen */ @Configuration public class IndexViewController implements WebMvcConfigurer {
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController(“/”).setViewName(“forward:index.html”); } } ```