import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class DefaultView extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
}
}
setOrder可以設置ViewController的優先級,強烈建議此處設置爲最高,這樣即使其他Controller中存在一樣的映射(即/),則仍會優先【步驟2】中ViewController。
在resource/static目錄下創建welcome.html文件用於作爲首頁進行展示,welcome中的內容根據需要進行編寫。
Spring Boot默認提供靜態資源目錄位置需置於classpath下, 文件名需符合下面規則:
- /static
- /public
- /resources
- /META-INF/resources
靈光一現想到web項目的默認頁面是直接放在webcontent下面, 雖然需要在web.xml配置, 雖然Spring Boot 並沒有web.xml但試試又不要錢於是
只要將index.html放在上面規則的目錄下, 在訪問項目的時候會被默認加載