1、概述
(1)概念
springmvc的处理器拦截器类似于servlet中的过滤器filter,用于对处理器进行预处理和后处理。开发者可以自己定义一些拦截器(必须实现HandlerInterceptor)来实现特定的功能
(2)过滤器与拦截器
拦截器是AOP思想的具体应用。
过滤器:
servlet规范的一部分,任何javaweb工程都可以使用,在url-pattern中配置了/*之后,可以对所有要访问的资源进行拦截
拦截器:
拦截器是springmvc框架自己的,只有使用了springmvc框架的工程才能使用,拦截器只会拦截访问的控制器方法,如果访问的是jsp/html/css/images是不会进行拦截的
2、拦截器演示
(1)在web.xml中对前端控制器和中文乱码的处理进行配置:
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
springmvc
/
EncodingFilter
pers.zhb.filter.EncodingFilter
encoding
utf-8
EncodingFilter
/*
书写过滤器:
public class EncodingFilter implementsFilter {public void init(FilterConfig filterConfig) throwsServletException {
}public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throwsIOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}public voiddestroy() {
}
}
(2)配置文件application:对视图解析器、拦截器等进行配置
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
(3)拦截器:
public class MyInterceptor implementsHandlerInterceptor {public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throwsException {
System.out.println("preHandle qian");//业务控制器执行前执行return true;//return true的话执行下一个拦截器,false的话中断客户端对拦截器的请求(要想中断请求返回false即可)
}public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throwsException {
System.out.println("postHandle hou");//业务控制器执行后,视图渲染前执行
}public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throwsException {
System.out.println("afterCompletion qingli");//视图渲染后执行
}
}
如果配置文件中配置了多个拦截器,那么在preHandle中的执行顺序是按照配置文件中的顺序,在postHandle和afterCompletion中是按照配置文件相反的顺序
(4)处理器:
@RestControllerpublic classTestController {
@GetMapping("/test1")publicString test1(){
System.out.println("123");return "ok";
}
}
(5)测试:
生命周期:
服务器启动时创建拦截器对象
访问业务控制器时,拦截器执行方法
服务器关闭时销毁拦截器对象
3、登录判断验证
(1)配置文件:applicationContext.xml,对视图解析器、拦截器进行配置
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
(2)页面:
起始页:
$Title$主页:
Title${username}
注销
登录页:
Login用户名:密码:
登录页面需要控制器的方法
(3)处理器:
importjavax.servlet.http.HttpSession;
@Controller
@RequestMapping("user")public classLoginController {
@RequestMapping("main")publicString main(){return "main";
}
@RequestMapping("gologin")publicString login(){return "login";
}
@RequestMapping("login")publicString login(HttpSession session, String username, String password, Model model){
session.setAttribute("userInfo",username);
model.addAttribute("username",username);return "main";
}
@RequestMapping("goOut")publicString goOut(HttpSession session){
session.removeAttribute("userInfo");return "main";
}
}
因为在WEB-INF下的所有页面或资源,只能通过servlet或controller进行访问,因此,需要书写相应的方法实现页面的跳转
(4)拦截器:
public class MyInterceptor implementsHandlerInterceptor {public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throwsException {
HttpSession session=request.getSession();if(request.getRequestURI().contains("gologin")){return true;
}if(request.getRequestURI().contains("login")){return true;
}if(session.getAttribute("userInfo")!=null){return true;
}
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);return false;
}
}
当有名为userInfo的session证明已经登录成功则不进行拦截,在跳转到登录页或者提交登录页信息的时候也不进行拦截。其他情况会被请求转发到登录页,如:未登录的时候在起始页点击首页。
(5)测试:
起始页:
登录页:
登录成功:
返回起始页点击首页:可以直接进入首页
点击注销后,返回起始页再次点击首页,跳转到登录页: