SpringMVC多个拦截器方法的执行顺序
工具/原料
SpringMVC
eclipse
方法/步骤
1、在我们程序中定下如下两个拦截器:<mvc:interceptors><mvc:interceptor><mvc:mappingpath="/springmvc/helloworld"></mvc:mapping><beanclass="com.gwolf.springmvc.interceptors.SecondInterceptor"></bean></mvc:interceptor><beanclass="com.gwolf.springmvc.interceptors.FirstInterceptor"></bean></mvc:interceptors>
2、声明第一个拦截器:packagecom.gwolf.springmvc.interceptors;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.web.servlet.HandlerInterceptor;importorg.springframework.web.servlet.ModelAndView;publicclassFirstInterceptorimplementsHandlerInterceptor{@OverridepublicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{System.out.println("FirstInterceptorpreHandle--------");returntrue;}@OverridepublicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{System.out.println("FirstInterceptorpostHandle--------");}@OverridepublicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{System.out.println("FirstInterceptorafterCompletion-------------");}}
3、声明第二个拦截器SecondInt娣定撰钠erceptor:packagecom.gwolf.springmvc.足毂忍珩interceptors;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.web.servlet.HandlerInterceptor;importorg.springframework.web.servlet.ModelAndView;publicclassSecondInterceptorimplementsHandlerInterceptor{@OverridepublicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{System.out.println("SecondInterceptorpreHandle--------");returntrue;}@OverridepublicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{System.out.println("SecondInterceptorpostHandle--------");}@OverridepublicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{System.out.println("SecondInterceptorafterCompletion-------------");}}
4、声明一个springmvc控制层:圬桦孰礅packagecom.gwolf.springmvc.handlers;importorg.霜杼厮贿springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RequestParam;importcom.gwolf.springmvc.domain.Person;@Controller@RequestMapping("/springmvc")publicclassHelloWorldController{/***使用@RequestMapping注解来映射请求的URL**返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver*视图解析器,会做如下的解析:通过prefix+returnVal+后缀这样的方式得到实际的物理视图,然后*做转发操作。*/WEB-INF/views/success.jsp*@return*/@RequestMapping(value="/helloworld/{id}",method=RequestMethod.GET)publicStringhello(@PathVariableIntegerid){System.out.println("testrestget:"+id);return"success";}@RequestMapping(value="/helloworld",method=RequestMethod.POST)publicStringhello(Personperson){System.out.println(person);return"success";}@RequestMapping(value="/helloworld/{id}",method=RequestMethod.DELETE)publicStringhelloDelete(@PathVariableIntegerid){System.out.println("testrestdelete:"+id);return"success";}@RequestMapping(value="/helloworld/{id}",method=RequestMethod.PUT)publicStringhelloPUt(@PathVariableIntegerid){System.out.println("testrestput:"+id);return"success";}}
5、执行任意一个jsp页面,查看程序的执行结果:
6、当有多个拦截器时,我们查看这些拦截器的执行顺序,拦截器会根据配置的顺序首先执行preHandle的方法: