SpringBoot 整合thymeleaf--跳轉首頁
阿新 • • 發佈:2021-05-06
方式一:通過Controller 跳轉實現
- 編寫controller
@Controller
public class IndexController {
@RequestMapping({"/","/index","/index.html"})
public String doIndex(){
return "index";
}
}
- 在resources 目錄新建template目錄(用於存放頁面)
- 測試
方式二:通過自定義配置實現
- 編寫一個配置類實現WebMvcConfigurer
@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");
}
}
這裡就不截圖了