【spring-boot】spring-boot项目中,通过JAVA配置类形式配置前台代码都重定向转发

以下是MyMvcConfig.java配置类的代码,可供参考:

package com.springboot.config;

import com.springboot.conponent.LoginHanderInterceptor;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author zhuhonggen
 * @version 创建时间:2019/08/06 21:05
 * @ClassName 类名称
 * @Description 类描述
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer adpater = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/login.html").setViewName("login");
                //使用重定向
                //registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //registry.addInterceptor(new LoginHanderInterceptor()).addPathPatterns("/**").excludePathPatterns("/","/login.html","/action/login","/error/404");
            }
        };
        return adpater;
    }

}

猜你喜欢

转载自www.cnblogs.com/jums/p/11323658.html