SpringBoot---页面跳转之WebMvcConfigurerAdapter

摘要:在springboot中定义自己的方法继承WebMvcConfigurerAdapter方法可以实现扩展springMvc功能,要全面实现接管springmvc就要在自己的方法上加上@EnableWebMvc注解。

  • 首先看WebMvcConfigurerAdapter部分源码:
    @Deprecated//看标色部分就是实现了WebMvcConfigurer接口  因此可以理解为什么说扩展springmvc功能
    public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
    
        /**
         * {@inheritDoc}
         * <p>This implementation is empty.
         */
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
        }
    
        /**
         * {@inheritDoc}
         * <p>This implementation is empty.
         */
    ......
  • 如何实现页面跳转(实质就是配置结果视图)
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
    @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文件夹下,不然没法找到访问异常如下:

原文地址:https://www.cnblogs.com/wangsr-suc/p/9096431.html