码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot---页面跳转之WebMvcConfigurerAdapter

时间:2018-05-27 16:48:58      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:ati   web   bst   nts   结果   abstract   如何   login   registry   

摘要:在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文件夹下,不然没法找到访问异常如下:

技术分享图片

 

SpringBoot---页面跳转之WebMvcConfigurerAdapter

标签:ati   web   bst   nts   结果   abstract   如何   login   registry   

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

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!