拦截器:顾名思义,就是对方法进行拦截,进行我们想要的加工,然后再进行返回。
这个时候你可能会问我,这不就和过滤器一样嘛,还学拦截器干嘛?
这里需要切记,过滤器和拦截器执行的时机是大大不一样的!
过滤器是对每一次的请求进行过滤处理。是在访问的途中。
拦截器是在进入控制单元前开始执行。
用一张图进行表示一下:
拦截器有三种方法:分别是:
preHandle:进入控制单元前的处理 ,作用在代码注释里有。
postHandle:数据处理结束,页面渲染前执行。
afterCompletion:页面渲染时执行。
知道了这么多,我们了解一下
拦截器如何配置?
普通的类去继承HandlerIntercepter
package com.bjsxt.inter;
import com.bjsxt.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
public class MyInterceptor1 implements HandlerInterceptor {
//进入控制单元前的方法预处理
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("进入了拦截器1 preHandle");
//作用一:处理表单中文乱码问题:
//httpServletRequest.setCharacterEncoding("utf-8");
//作用二:功能维护,跳转到其他的页面:
//httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/wh.jsp");
//在这里可以判断是否进入控制单元 true:进入, false:不进入
return true;
}
//在数据处理结束,页面渲染之前执行
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//作用一 : 敏感字符的替换
//req.setAttribute就好比将值存入map集合,我们可以在controller中放入一个map集合
//按照键值对的情况读取数据
// Map<String, Object> map = modelAndView.getModel();
// //这里可以读到controller存入mao集合key值=ok的数据
// String ok = (String) map.get("ok");
// //对数据处理
// String jk=ok+"ojbk";
// //这样显示在页面可以进行修改
// map.put("ok",jk);
//作用二:新研发功能的测试
/*resp.sendRedirect(req.getContextPath()+"/main2.jsp");
return;*/
/*modelAndView.setViewName("redirect:/main2.jsp");*/
System.out.println("进入了拦截器1 postHandle");
}
//页面渲染结束执行
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("进入了拦截器1 afterCompletion");
}
}
mvc.xml的配置文件如下:
<mvc:interceptors>
<mvc:interceptor>
<!-- 选定控制单元-->
<mvc:mapping path="/login"/>
<!-- 选定拦截器-->
<bean id="inter1" class="com.bjsxt.inter.MyInterceptor1"></bean>
</mvc:interceptor>
</mvc:interceptors>
控制单元:
@RequestMapping("login")
public String login(Map<String,Object>map,String uname, String pwd, HttpSession session, HttpServletRequest request){
System.out.println("------------进入控制单元");
User user=emailService.login(uname,pwd);
System.out.println("-------------控制单元service登录完毕");
session.setAttribute("user",user);
map.put("ok","色情信息");
return "forward:/main.jsp";
}
执行顺序如图所示:
当有多个拦截器如何配置呢?
新加一个拦截器:
package com.bjsxt.inter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("preHandle2");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle2");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion2");
}
}
mvc.xml的配置如下:
<mvc:interceptors>
<bean id="inter1" class="com.bjsxt.inter.MyInterceptor1"></bean>
<bean id="inter2" class="com.bjsxt.inter.MyInterceptor2"></bean>
</mvc:interceptors>
多个拦截器的执行顺序如下:
(tips:他是按照配置的顺序开始执行)
为了理解我们可以把两个拦截器想成一个回字,仔细想想拦截器是按照配置顺序,所以肯定先进最外面的,也就是最先配置的拦截器。然后再进最下面的拦截器:
最外面的是先配置的拦截器 里面的是后配置的拦截器
pre1--->pre2--->控制单元方法 --->post2--->post1---->after2---->after1
如果我在配置一个过滤器呢?只有一个拦截器
package com.bjsxt.filter;
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init");
}
@Override
public void destroy() {
System.out.println("destory");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("doFilter前");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("doFilter后");
}
}
web.xml配置过滤器:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>f</filter-name>
<filter-class>com.bjsxt.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>