SpringBoot拦截器
1、定义拦截器(与SpringMVC的定义方式一致)
package com.mingde.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object arg2) throws Exception {
System.out.println("MyInterceptor01--->preHandle()执行控制器之前调用此方法....");
//返回true则放行,返回false则将其拦截住
return true;
}
@Override
public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("MyInterceptor01--->postHandle()执行控制器之后且在渲染视图前调用此方法....");
}
@Override
public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object arg2, Exception arg3)
throws Exception {
System.out.println("MyInterceptor01--->afterCompletion()执行控制器之后且在完成渲染视图后调用此方法....");
}
}
2、注册拦截器
package com.mingde.configurer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.mingde.interceptor.MyInterceptor;
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
//重写添加拦截器的方法(添加调用的拦截器,并指定拦截的目标)
@Override
public void addInterceptors(InterceptorRegistry registry) { //注册拦截器
//代表指定拦截所有路径的资源.并且排除掉xxx(不拦截xxx)
//registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("xxx");
//addPathPatterns("/**")代表指定拦截所有路径的资源
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
SpringBoot添加视图控制器
在上面的类中补习addview方法即可
package com.mingde.configurer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.mingde.interceptor.MyInterceptor;
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
//重写添加拦截器的方法(添加调用的拦截器,并指定拦截的目标)
@Override
public void addInterceptors(InterceptorRegistry registry) { //注册拦截器
//代表指定拦截所有路径的资源.并且排除掉xxx(不拦截xxx)
//registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("xxx");
//addPathPatterns("/**")代表指定拦截所有路径的资源
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
//加载静态资源
/*@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// TODO Auto-generated method stub
super.addResourceHandlers(registry);
}*/
//重写添加视图控制器(不用写controller就可以跳转至指定的jsp,比如:http://localhost:90/tologin)
//此时就跳转到/WEB-INF/jsp/list.jsp页面
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("tologin").setViewName("list");
super.addViewControllers(registry);
}
}