spring拦截器

拦截器中是实现了HandlerInterceptor中的方法

public class Logincontroller implements HandlerInterceptor{
	//使用目标方法之前执行此方法
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object arg2) throws Exception {
		//获取session对象
		HttpSession session = req.getSession();
		Object user = session.getAttribute(Constant.USER_IN_SESSION);
		if(user==null){
			resp.sendRedirect("/login");
			return false;
		}
		
		return true;
	}
	//执行完页面之后执行此方法
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	//执行目标方法之后执行此方法
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}

true代表放行,false代表拦截
最后还要进行mvc的配置

//可以配置多个拦截器
<mvc:interceptors>
		<mvc:interceptor>
		//拦截的路径
			<mvc:mapping path="/**"/>
			//排除这些
			<mvc:exclude-mapping path="/reg"/>
			<mvc:exclude-mapping path="/login"/>
			
		//拦截器的类的全限定名
		<bean class="cn.itsource.controller.Logincontroller"></bean>
		
		</mvc:interceptor>

	</mvc:interceptors>
	
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页