Spring Boot -- 视图解析器,资源处理器,拦截器

package com.id0304.ssm.config;

import com.id0304.ssm.interceptor.IndetifyHandlerInterceptor;
import com.id0304.ssm.interceptor.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {
    //资源处理器,当请求一些静态资源时会默认在以下路径 寻找
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    //视图解析器在url访问时会自动 补上 前后缀,记得这个不是WebMvcConfigurationSupport类下的,需要手动注入工厂
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/html/");
        internalResourceViewResolver.setSuffix(".html");
        return internalResourceViewResolver;
    }

    //视图控制器,在访问对应的url时会跳转到对应的定义的新视图,受视图解析器的影响
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
    }

    //拦截器,路径指的是实际代码上写的路径,比如页面引入一个图片,因为有视图解析器,我们不用写成localhost:8080/static/img/**.png

    /**
     * 拦截器,路径指的是实际代码上写的路径,比如页面引入一个图片,因为有视图解析器,
     * 我们不用写成/static/img/**.png而在页面上的路径是/img/**.png
     * 这时拦截器要排除图片资源路径就要写成/img/**.png,随实际请求路径变化
     *
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/", "/user/toLogin.action", "/html/login.html");
        registry.addInterceptor(new IndetifyHandlerInterceptor()).addPathPatterns("/user/toIndex.action");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值