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");
}
}
Spring Boot -- 视图解析器,资源处理器,拦截器
最新推荐文章于 2023-08-06 23:08:23 发布