1、拦截器概念和Struts2一致
2、实现拦截器(程序是先执行dispatcherServlet,然后执行拦截器,然后执行controller的)
a) 实现HandleInterceptor接口,(实现它的三个方法,注意三个方法分别在什么时候执行)
public class MyInterceptor implements HandlerInterceptor{ //在dispatcherServlet处理后执行 做清理工作 @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { } //在请求处理的方法之后执行,处理后看方法的参数也可以改变它的modelandview @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { System.out.println("--------处理后-----------"); } //在请求处理的方法之前执行(比喻登录,要在登录方法之前执行) //如果返回true执行下一个拦截器,如果返回false那么不执行下一个拦截器 @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { System.out.println("---------处理前----------"); return true; //返回false,只会在controller执行前执行,后面就没有后续的动作了 } }上面的代码
b) 配置拦截器
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置渲染(解析)器 --> <!-- 注解扫描,自动扫描这个包下面的注解 --> <context:component-scan base-package="cn.sxt.controller"></context:component-scan> <!-- 拦截器的配置,interceptors表示可以配置多个拦截器 拦截器相比filter可以拦截的更加细腻一些--> <mvc:interceptors> <!-- 表示具体的某一个拦截器 --> <mvc:interceptor> <!-- path表示可以拦截指定的一个url /**表示包括路径及其子路径--> <!-- 如果是/admin/*-拦截的是/admin/add,/admin/list etc.(etc是等等的意思)/admin/user/sdd不会拦截--> <!-- 24 如果是/admin/**拦截》上面的都可以拦截,拦截路径及其子路径--> <mvc:mapping path="/**"/> <!-- bean其实配置的就是拦截器 --> <bean class="cn.sxt.interceptor.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
上面的代码
3、问题一:如果被拦截----能否到达指定的页面
使用HttpServletResponse或者HttpServletRequest可以实现重定向
controller执行之前经过拦截器的一个方法的执行代码
//在请求处理的方法之前执行(比喻登录,要在登录方法之前执行) //如果返回true执行下一个拦截器,如果返回false那么不执行下一个拦截器 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("---------处理前----------"); response.sendRedirect(request.getContextPath()+"/index.jsp"); return true; //返回false,只会在controller执行前执行,后面就没有后续的动作了 }
4、问题二:实现登录拦截器(拦截器的应用)
配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置渲染(解析)器 --> <!-- 注解扫描,自动扫描这个包下面的注解 --> <context:component-scan base-package="cn.sxt.controller"></context:component-scan> <!-- 拦截器的配置,interceptors表示可以配置多个拦截器 拦截器相比filter可以拦截的更加细腻一些--> <mvc:interceptors> <!-- 表示具体的某一个拦截器 --> <mvc:interceptor> <!-- 这里不配置,默认就是所有都被拦截 --> <!-- 不仅有这种标签,还有除了那个不拦截外,其它都拦截标签mvc:exclude-mapping --> <mvc:mapping path="/**"/> <!-- bean其实配置的就是拦截器 --> <bean class="cn.sxt.interceptor.LoginInterceptor"> <property name="allowedPass"> <list> <value>login.do</value> //<value>add.do</value> </list> </property> </bean> </mvc:interceptor> </mvc:interceptors> </beans>