拦截器 不能改变参数值,适合权限验证等 校验的时候。跟过滤器差不多,只是范围和时机不同
今天遇到的需求是要查看请求中是否有分页参数,如果有就不处理,如果没有就注入默认值。发现拦截器不合适就改用了aop;
必须实现接口HandlerInterceptor 或者继承适配器HandlerInterceptorAdapter
拦截所有的请求
<beans>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="interceptors">
<list>
<ref bean="officeHoursInterceptor" />
</list>
</property>
</bean>
<bean id="officeHoursInterceptor" class="samples.TimeBasedAccessInterceptor">
<property name="openingTime" value="9" />
<property name="closingTime" value="18" />
</bean>
</beans>
拦截有限的请求
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/admin/**" />
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/secure/*" />
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors>