1.拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new FirstHandlerInterceptor()).addPathPatterns("/**");
}
public class FirstHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
/*
返回true则继续执行控制器,如果返回false则停止运行
*/
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
2.跨域访问
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
3.格式化
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
}
4.视图映射
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/file").setViewName("/test_file");
}
相当于
@GetMapping(value = "/file")
public String file(){
return "test_file";
}