SpringBoot搭建项目几个小坑

WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口,WebMvcConfigurerAdapter 抽象类是对WebMvcConfigurer接口的简单抽象(增加了一些默认实现),但在在SpringBoot2.0及Spring5.0中WebMvcConfigurerAdapter已被废弃 。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport,方式一实现WebMvcConfigurer接口(推荐),方式二继承WebMvcConfigurationSupport类,原来的WebMvcConfigurerAdapter已经被摒弃。

我的目录结构如下

原先应用启动后在login界面,一直报错 “Request method 'GET' not supported”,修改Controller中Post方法没有用,添加@Configuration后解决。

另外添加"/js/**","/css/**","/images/**"到下面代码中后,static下资源才得以正常加载。

@Configuration //必须加,不然会报" Request method 'GET' not supported"
public class MyWebMvcConfigurerAdapter  implements WebMvcConfigurer{

    @Override
    public void addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin", "/login","/js/**","/css/**","/images/**");
    }

    @Override
    public void addViewControllers(org.springframework.web.servlet.config.annotation.ViewControllerRegistry registry) {
        registry.addViewController("/toLogin").setViewName("login");
    }

}

说明:addPathPatterns("/**")对所有请求都拦截,但是排除了/toLogin/login请求的拦截。

参考博客:https://blog.csdn.net/fmwind/article/details/81235401

猜你喜欢

转载自blog.csdn.net/ifidieyoung/article/details/84990781