- 拦截器是在用户和具体操作的Action之间做一个屏障
- 保证提交到Action上的数据是真实有效的数据
实现拦截器的操作处理接口:org.springframework.web.servlet.HandlerInterceptor
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception:在某一个控制器的方法执行之前调用
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception:正在执行某一个控制器中的方法
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception:控制器的方法执行完毕之后进行调用
拦截器中使用了一个程序类:org.springframework.web.method.HandlerMethod
- public Object getBean():返回触发此拦截器的程序类(*Action)
- public Method getMethod():取得具体操作Action中的方法的Method对象
- public Class<?> getBeanType():取得触发此拦截器程序类的Class对象
- public MethodParameter[] getMethodParameters():取得所有提交到此方法上的参数
所有用户请求时提交的参数,在拦截器中使用org.springframework.core.MethodParameter程序类进行接收:
- public Method getMethod():取得调用的方法对象
- public Class<?> getParameterType():取得提交到此Action上的参数名称
- public String getParameterName():取得此参数的类型
Spring拦截器的简单应用
1、定义一个拦截器
package cn.liang.util.validate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class ValidationInterveptor implements HandlerInterceptor {
Logger log = Logger.getLogger(ValidationInterveptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
log.info("**** preHandle:" + handler.getClass());
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
log.info("**** postHandle:" + handler.getClass());
log.info("**** postHandle-ModelAndView:" + modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
log.info("**** afterCompletion:" + handler.getClass());
}
}
2、配置applicationContext-mvc.xml文件
<mvc:interceptors> <!-- 定义拦截器栈,可以定义有多个拦截器 -->
<mvc:interceptor> <!-- 定义某一个具体的拦截器 -->
<mvc:mapping path="/pages/**/*.action"/> <!-- 该拦截器针对于所有路径下的action -->
<!-- 定义该拦截器使用的拦截器处理程序类,必须是HandlerInterceptor子类 -->
<bean class="cn.liang.util.validate.ValidationInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
3、编写一个Action
package cn.liang.action;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/pages/emp/*")
public class EmpAction{
private Logger log = Logger.getLogger(EmpAction2.class) ;
@RequestMapping("remove")
public ModelAndView remove(@RequestParam(value="empno",defaultValue="10") int eid){
System.out.println("输出的eid结果:"+ eid * 2);
return null;
}
}
4、启动程序,输出结果
2018-12-12 16:28:03,987 INFO [cn.liang.util.validate.ValidationInterveptor] - **** preHandle:class org.springframework.web.method.HandlerMethod
输出的eid结果:20
2018-12-12 16:28:03,988 INFO [cn.liang.util.validate.ValidationInterveptor] - **** postHandle:class org.springframework.web.method.HandlerMethod
2018-12-12 16:28:03,988 INFO [cn.liang.util.validate.ValidationInterveptor] - **** postHandle-ModelAndView:null
2018-12-12 16:28:03,988 INFO [cn.liang.util.validate.ValidationInterveptor] - **** afterCompletion:class org.springframework.web.method.HandlerMethod