欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC拦截器

程序员文章站 2022-05-28 17:49:11
...

注意:拦截器是框架中的技术,拦截的是controller中执行的方法

springmvc配置文件:

        <!-- 配置SpringMVC拦截器 -->
	<mvc:interceptors>
		<!-- 一个拦截器 -->
		<mvc:interceptor>
			<!-- 要拦截的配置 -->
			<mvc:mapping path="/*" />
			<!-- 设置不拦截的配置 -->
			<mvc:exclude-mapping path="/login.do" />
			<!-- 配置拦截器 -->
			<bean class="com.zhengqing.controller.MyInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

自定义类继承HandlerInterceptor:

public class MyInterceptor implements HandlerInterceptor {
	
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object object) throws Exception {
		System.out.println("===拦截===");
		return true;//false为拦截,true为放行
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
		System.out.println("===处理完请求之后===");
	}

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
		System.out.println("===DispatcherServlet完结之后调用===");
	}
	
}

 

相关标签: SpringMVC拦截器