首页 > 编程语言 > 详细

Spring Boot 对请求的映射

时间:2019-02-23 11:47:00      阅读:436      评论:0      收藏:0      [点我收藏+]

 

在SpringBoot中对请求资源的映射有三种方式:

  1.通过编写一个Controller请求,获得客户端发送过来的请求就转发出去

 //通过这种方式可以来映射页面请求路径
    @PostMapping("/hello")
    public String hello(){
        
        return "login";//如果使用thymeleaf引擎,会去classpath:/public找对应的html
    }

    2.通过实现WebMvcConfigurerAdapter但是在2.x.x中已过时

  

  @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        // super.addViewControllers(registry);
        //浏览器请求 首页,将跳转到 thymeleaf下的login页面。
        registry.addViewController("/index.html").setViewName("login");
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/main.html").setViewName("dashboard");
    }

    /**
     * 通过另一种方式来配置虚拟路径
     * @return adapter
     */
    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //添加到Spring容器中
   public WebMvcConfigurerAdapter MyWebMvcConfigurerAdapter(){

        //内部类
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
            }

            //WebMvcConfigurerAdapter中注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").
                        excludePathPatterns("/index.html","/","/user/login","/webjars/**","/asserts/**");
            }

        };
        return adapter;

}

   3.通过实现WebMvcConfigurer方式,复写addInterceptors方法,和第二种方式一样(推荐

 

Spring Boot 对请求的映射

原文:https://www.cnblogs.com/lcaiqin/p/10421914.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!