Jim
Jim

Reputation: 1

Spring MVC Annotations and HandleInterceptors

I am on a project using Spring 3.x MVC, and have implemented our controllers using annotations. We recently have a requirement to implement HandlerInterceptors, to which I have had some problems. When I specify in my configuration (dispatcher-sevlet.xml), the interceptor

<bean id="myInterceptor" class="com.myProject.controllers.MyInterceptor" />
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  <property name="interceptors">
    <list><ref bean="myInterceptor"/></list>
  </property>
</bean>

then all is well, that is, any URL matches hits the myInterceptor code. When I try to add

 <property name="mappings">
    <props>
      <prop key="/addFile.request">myFileController
      </prop>
    </props>
  </property>

then I never hit the myInterceptor code...I have also tried to implement the above mapping code using @RequestMapping annotations.

Upvotes: 0

Views: 3104

Answers (1)

jimr
jimr

Reputation: 11230

It's easier to use the <mvc:interceptors> tag to configure interceptors if you're using annotation-based configuration.

E.g

<mvc:interceptors>
 <!-- This runs for all mappings -->
 <bean class="my.package.GlobalInterceptor"/>
 <mvc:interceptor>
  <!-- This one only runs for a specific URL pattern -->
  <mvc:mapping path="/admin/*"/>
   <bean class="my.package.AdminInterceptor"/>
  </mvc:interceptor>
</mvc:interceptors>

Upvotes: 3

Related Questions