SpringBoot---頁面跳轉之WebMvcConfigurerAdapter
阿新 • • 發佈:2018-05-27
inf apt col fig 分享 繼承 ima 部分 配置結果
摘要:在springboot中定義自己的方法繼承WebMvcConfigurerAdapter方法可以實現擴展springMvc功能,要全面實現接管springmvc就要在自己的方法上加上@EnableWebMvc註解。
- 首先看WebMvcConfigurerAdapter部分源碼:
@Deprecated//看標色部分就是實現了WebMvcConfigurer接口 因此可以理解為什麽說擴展springmvc功能 public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { /** * {
- 如何實現頁面跳轉(實質就是配置結果視圖)
@Configuration public class MyMvcConfig extendsWebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/cn.itcast").setViewName("login"); } }
//第二種方法:
@Bean public WebMvcConfigurerAdapter webMvcConfigurerAdapter() { WebMvcConfigurerAdapter adapter=new WebMvcConfigurerAdapter() { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/cn.itcast").setViewName("login"); } }; return adapter; }
其中addViewController方法可設置映射路徑 / 代表當前項目,後面的自定義,setViewName設置要被映射的html文件
註意:此文件需要在resourse包下的template文件夾下,不然沒法找到訪問異常如下:
SpringBoot---頁面跳轉之WebMvcConfigurerAdapter