1. 程式人生 > 其它 >SpringBoot 整合thymeleaf--跳轉首頁

SpringBoot 整合thymeleaf--跳轉首頁

方式一:通過Controller 跳轉實現

  1. 編寫controller
@Controller
public class IndexController {

    @RequestMapping({"/","/index","/index.html"})
    public String doIndex(){

        return "index";
    }
}
  1. 在resources 目錄新建template目錄(用於存放頁面)

在這裡插入圖片描述

  1. 測試
    在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

方式二:通過自定義配置實現

  1. 編寫一個配置類實現WebMvcConfigurer
    介面 重寫addViewControllers(ViewControllerRegistry registry)方法
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //配置對映頁面
        registry.addViewController("/").setViewName("index"
); registry.addViewController("/index").setViewName("index"); registry.addViewController("/index.html").setViewName("index"); } }

這裡就不截圖了