重定向问题解决的笨方法

业务逻辑完成之后如保存、登陆成功等关键业务逻辑,用重定向到方式完成跳转。

可以采用在Controller中直接拼装"redirect"的方式,用ModelAndView 和 字符串拼装都可以。

还有一种,就是自定义@Configurtation 组件,注册一个 WebMvcConfigurationAdapter,该方式只是新增,不会影响Springboot原有组件

注意:该@Configuration 组件,不能使用 @EnableWebMvc 注解,否则SrpingBoot 自动加载的 WebMvc 会失效。

代码片段如下:在Controller中 使用 return "/result.html" 之后,就会跳转到/success 路径中,完成重定向转发

@Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/result.html").setViewName("/success");
            }
        };
        return webMvcConfigurer;
    }

猜你喜欢

转载自www.cnblogs.com/KennyWang0314/p/12325579.html
今日推荐