SpingMVC拦截器实例

package com.llf.interceptor;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

/*

* 拦截器demo

*/

public class TextInterceptor implements HandlerInterceptor{

@Override

//最后执行,当preHandle方法返回为true的时候才会执行

public void afterCompletion(HttpServletRequest arg0,

HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

// TODO Auto-generated method stub

System.out.println("afterCompletion方法");

}

@Override

//controller执行完之后执行,当preHandle方法返回为true的时候才会执行

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2, ModelAndView mav) throws Exception {

// TODO Auto-generated method stub

String s = "拦截器postHandel方法";

mav.addObject("s", s);

mav.setViewName("index");

}

@Override

//在controller方法执行完之前才会执行

public boolean preHandle(HttpServletRequest request, HttpServletResponse response,

Object arg2) throws Exception {

// TODO Auto-generated method stub

// 登陆功能验证

// System.out.println("preHandle方法");

// int i = 2;

// if(i == 1){

// return true;

// }else{

// request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);

// return false;

// }

return true;

}

}

spring-mvc.xml

你可能感兴趣的:(SpingMVC拦截器实例)