栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot 使用addviewController(),No mapping for GET xx.css

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot 使用addviewController(),No mapping for GET xx.css

Spring Boot 使用addviewController()实现无业务逻辑跳转,,出现静态资源映射找不到的情况 。No mapping for GET xx.css
WebMvcConfigurerAdapter方法过时
但是自从Spring Boot2.0的版本之后这个方法就过时了,由以下两种方法来实现。

①implements WebMvcConfigurer(官方推荐)

②extends WebMvcConfigurationSupport

/
* @deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made
 * possible by a Java 8 baseline) and can be implemented directly without the
 * need for this adapter
 */
@Configuration
public class MyMvcConfigNew extends WebMvcConfigurerAdapter {
 
 
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
    }
}

以下为实际的项目代码:

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/atguigu").setViewName("success");
    }

    @Bean
    public MyMvcConfig webMvcConfigurationSupport() {
        MyMvcConfig webMvcConfigurationSupport = new MyMvcConfig() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }

            // 注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                // /** 拦截任意界面的任意请求
                // 静态资源: *.css , *.js
                //springBoot 已经做好了静态资源映射
//                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
//                    .excludePathPatterns("/index.html", "/user/login", "/static/css/**", "/webjars/**");

            }
        };
        return webMvcConfigurationSupport;
    }

    // 将我们编写的区域信息解析器放到容器中
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781795.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号