SpringBoot學習日誌之DAY05springboot默認首頁設置

目錄

 

什麼是默認首頁

配置springboot默認首頁

方式一:通過controller指定RequestMapping路徑爲(/)

方式二:通過自定義配置類實現WebMvcConfigurer進行設置

方式三:直接將index.html頁面放在templates下面就行了


什麼是默認首頁

    在啓動WEB項目之後,在瀏覽器輸入:127.0.0.1:8080 會自動進入的界面就是默認首頁

    在springMvc當中,直接在web.xml當中進行如下配置就會將webapp下的index.html當成默認首頁

 <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
 </welcome-file-list>

配置springboot默認首頁

  在springboot當中不存在web.xml,那麼怎麼配置默認首頁呢?

  在網上搜索了很多的資料,發現兩種方式:備註:以下所有方式都是使用的thymeleaf前端模板並沒有使用jsp

#thymeleaf 配置信息
#取消緩存,修改HTML界面後立即生效,不然會讀取緩存,沒有變化
spring.thymeleaf.cache=false
#下面5個配置信息都是常用的默認的配置信息,如果不需要修改,不寫也行
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.servlet.content-type=text/html

 

方式一:通過controller指定RequestMapping路徑爲(/)

@Controller
public class IndexController {

    @RequestMapping("/")
    public  String index(){
        return "test/index";
    }
}

方式二:通過自定義配置類實現WebMvcConfigurer進行設置

@Configuration
public class IndexView implements WebMvcConfigurer{
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/test/index");
    }
}

方式三:直接將index.html頁面放在templates下面就行了

正常Springboot啓動信息如下:

在templates下面放入index.html文件後的啓動信息如下:

在打印信息當中可以看到自動添加歡迎界面index

具體原因是因爲在:

WebMvcAutoConfiguration這個類中進行了相關的映射

參考資料:https://blog.csdn.net/fmwind/article/details/81235401

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章