<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.springmvc.intercepter.FirstInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/springmvcTest/testRequestMapping"/>
<mvc:mapping path="/springmvcTest/testPathVariable/{id}"/>
<bean class="com.springmvc.intercepter.SecondInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
以上配置,
自定义拦截器FirstInterceptor,所有分发Controller都执行。
自定义拦截器SecondInterceptor,path=”/springmvcTest/testRequestMapping”和path=”/springmvcTest/testPathVariable/{id}”执行
分别点击RequestMapping和PathVariable,控制台显示结果如下:
FirstInterceptor.preHandle()
SecondInterceptor.preHandle()
testRequestMapping
SecondInterceptor.postHandle()
FirstInterceptor.postHandle()
SecondInterceptor.afterCompletion()
FirstInterceptor.afterCompletion()
FirstInterceptor.preHandle()
SecondInterceptor.preHandle()
testPathVariable
SecondInterceptor.postHandle()
FirstInterceptor.postHandle()
SecondInterceptor.afterCompletion()
FirstInterceptor.afterCompletion()
分别点击RequestParam、CookieValue、RequestHeader,控制台显示如下:
FirstInterceptor.preHandle()
testCookieValue
FirstInterceptor.postHandle()
FirstInterceptor.afterCompletion()
以上,SecondInterceptor不起作用,因为配置是:
<mvc:interceptor>
<mvc:mapping path="/springmvcTest/testRequestMapping"/>
<mvc:mapping path="/springmvcTest/testPathVariable/{id}"/>
<bean class="com.springmvc.intercepter.SecondInterceptor" />
</mvc:interceptor>
本文详细介绍了 SpringMVC 中的拦截器配置方式及其工作流程。通过具体示例展示了如何配置不同类型的拦截器以实现对特定路径请求的预处理、后处理及完成后的操作。
451

被折叠的 条评论
为什么被折叠?



