Announcement Announcement Module
Collapse
No announcement yet.
Interceptor doesn't work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Interceptor doesn't work

    Hi all,
    I am working with Spring 3.1.0.M2 and I configured controllers by @Controller and @RequestMapping annotations.

    I want to intercept all client request (*) with an Interceptor which implements HandlerInterceptor class which belongs to spring-framework.

    I configured all what I need in my xml configuration file:
    Code:
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
      <property name="interceptors">
        <list>
          <bean class="it.mypackage.MyInterceptor" />
        </list>
      </property>
    </bean>
    and I wrote myInterceptor class which implements HandlerInterceptor

    Code:
    public class MyInterceptor implements HandlerInterceptor {
    	
      @Override
      public void afterCompletion(
      ...
    but the requests aren't intercepted by MyInterceptor, maybe there is a mistake in the configuration file. Anyone could help me?

    Thanks in advance,

    Enrico

  • #2
    And in which configuration file did you add the DefaultAnnotationHandlerMapping? It should be in the context created by the DispatcherServlet NOT the one create by the ContextLoaderListener. If that doesn't solve it post your configuration and your interceptor...

    Comment


    • #3
      Hi Marten, thx a lot 4 your help,

      Originally posted by Marten Deinum View Post
      And in which configuration file did you add the DefaultAnnotationHandlerMapping? It should be in the context created by the DispatcherServlet NOT the one create by the ContextLoaderListener. If that doesn't solve it post your configuration and your interceptor...
      Yes, my DispatcherServlet has name "spitter" as <servlet-name> in web.xml and I wrote the interceptor configuration in spitter-servlet.xml. In web.xml I declare
      <url-pattern>*.do</url-pattern> also, but I don't declare anymore about pattern when I defined interceptors, could it be an mistake? Do I have to declare it?

      Comment


      • #4
        As I mentioned earlier post your configuration...

        Comment


        • #5
          Ok, thx Marten, this my web.xml:

          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
          
          	<display-name>Spring-3</display-name>
          
          	<context-param>
          		<param-name>contextConfigLocation</param-name>
          		<param-value>
          			/WEB-INF/myApplicationContext.xml
          		</param-value>
          	</context-param>
          
            	<listener>
          		<listener-class>
          			org.springframework.web.context.ContextLoaderListener
          		</listener-class>
          	</listener>
          
              <servlet>
          	    <servlet-name>spitter</servlet-name>
          	    <servlet-class>
          			org.springframework.web.servlet.DispatcherServlet
          		</servlet-class>
          	    <load-on-startup>1</load-on-startup>
              </servlet>
          
              <servlet-mapping>
          	    <servlet-name>spitter</servlet-name>
          	    <url-pattern>*.do</url-pattern>
              </servlet-mapping>
           
          </web-app>
          this my spitter-servlet.xml:

          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          
          <beans 
              xmlns="http://www.springframework.org/schema/beans"
          	xmlns:p="http://www.springframework.org/schema/p" 
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xmlns:mvc="http://www.springframework.org/schema/mvc"
          	xmlns:context="http://www.springframework.org/schema/context"
          	xsi:schemaLocation="http://www.springframework.org/schema/mvc
          	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          	http://www.springframework.org/schema/beans
          	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          	http://www.springframework.org/schema/context
          	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
          	
          	<!-- Per le richieste statiche -->
          	<mvc:resources mapping="/resources/**" location="/resources/" />
          	
          	<mvc:annotation-driven/>
          	 
          	<context:component-scan base-package="it.intesys.spring" />
          
          	<bean id="configurationLoader"  class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader"/>  
              
                  <bean id="validator" class="org.springmodules.validation.bean.BeanValidator" p:configurationLoader-ref="configurationLoader"/>  
          
          	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
          		<property name="interceptors">
          			<list>
          				<bean class="it.intesys.spring.interceptor.MyInterceptor" />
          			</list>
          		</property>
          	</bean>
          
          </beans>
          and finally this is MyInterceptor.java

          Code:
          package it.intesys.spring.interceptor;
          
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          
          import org.springframework.web.servlet.HandlerInterceptor;
          import org.springframework.web.servlet.ModelAndView;
          
          public class MyInterceptor implements HandlerInterceptor {
          	
          	@Override
          	public void afterCompletion(HttpServletRequest arg0,
          			HttpServletResponse arg1, Object arg2, Exception arg3)
          			throws Exception {
          
          		System.out.println("afterCompletion");
          		
          	}
          
          	@Override
          	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
          			Object arg2, ModelAndView arg3) throws Exception {
          		
          		System.out.println("postHandle");
          		
          	}
          
          	@Override
          	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
          			Object arg2) throws Exception {
          
          		System.out.println("preHandle");
          		
          		return false;
          	}
          
          }
          and also post my controller:

          Code:
          package it.intesys.spring.mvc;
          
          import it.intesys.spring.bean.Book;
          
          import java.util.Map;
          
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.stereotype.Controller;
          import org.springframework.validation.BindingResult;
          import org.springframework.validation.Validator;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RequestMethod;
          
          @Controller
          @RequestMapping({"/homecontroller"})
          public class MyHomeController 
          {
          
          	@Autowired
          	private Validator validator;  
              
                  public void setValidator(Validator validator) {  
                     this.validator = validator;  
                  }  
          
          	@RequestMapping(value="/book.do", method=RequestMethod.GET)
          	public String prepareBookForm(Map<String, Object> model) {
          		
          		model.put("book", new Book());
          		
          		return "home";
          		
          	}
          
          }

          Comment


          • #6
            Thx Marten, I post my configuration:

            web.xml:
            Code:
            	<context-param>
            		<param-name>contextConfigLocation</param-name>
            		<param-value>
            			/WEB-INF/myApplicationContext.xml
            		</param-value>
            	</context-param>
            
              	<listener>
            		<listener-class>
            			org.springframework.web.context.ContextLoaderListener
            		</listener-class>
            	</listener>
            
                <servlet>
            	    <servlet-name>spitter</servlet-name>
            	    <servlet-class>
            			org.springframework.web.servlet.DispatcherServlet
            		</servlet-class>
            	    <load-on-startup>1</load-on-startup>
                </servlet>
                
                <servlet-mapping>
            	    <servlet-name>spitter</servlet-name>
            	    <url-pattern>*.do</url-pattern>
                </servlet-mapping>
            spitter-servlet.xml
            Code:
            <?xml version="1.0" encoding="UTF-8"?>
            
            <beans 
                xmlns="http://www.springframework.org/schema/beans"
            	xmlns:p="http://www.springframework.org/schema/p" 
            	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            	xmlns:mvc="http://www.springframework.org/schema/mvc"
            	xmlns:context="http://www.springframework.org/schema/context"
            	xsi:schemaLocation="http://www.springframework.org/schema/mvc
            	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            	http://www.springframework.org/schema/beans
            	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            	http://www.springframework.org/schema/context
            	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
            	
            	<!-- Per le richieste statiche -->
            	<mvc:resources mapping="/resources/**" location="/resources/" />
            	
            	<mvc:annotation-driven/>
            	 
            	<context:component-scan base-package="it.intesys.spring" />
            
            	<bean id="configurationLoader"  class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader"/>  
                
                    <bean id="validator" class="org.springmodules.validation.bean.BeanValidator" p:configurationLoader-ref="configurationLoader"/>  
            
            	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            		<property name="interceptors">
            			<list>
            				<bean class="it.intesys.spring.interceptor.MyInterceptor" />
            			</list>
            		</property>
            	</bean>
            
            </beans>
            and, finally, MyInterceptor.java:

            Code:
            package it.intesys.spring.interceptor;
            
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;
            
            import org.springframework.web.servlet.HandlerInterceptor;
            import org.springframework.web.servlet.ModelAndView;
            
            public class MyInterceptor implements HandlerInterceptor {
            	
            	@Override
            	public void afterCompletion(HttpServletRequest arg0,
            			HttpServletResponse arg1, Object arg2, Exception arg3)
            			throws Exception {
            
            		System.out.println("afterCompletion");
            		
            	}
            
            	@Override
            	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            			Object arg2, ModelAndView arg3) throws Exception {
            		
            		System.out.println("postHandle");
            		
            	}
            
            	@Override
            	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            			Object arg2) throws Exception {
            
            		System.out.println("preHandle");
            		
            		return false;
            	}
            
            }

            Comment


            • #7
              You use mvc:annotation-driven which overrides your configuration... I suggest a read of the reference guide how to register interceptors with the namespace...

              Comment


              • #8
                Thx a lot Marten, you're right and I found the solution:

                Code:
                        ...
                	<mvc:annotation-driven/>
                	 
                	<mvc:interceptors>
                		<bean class="it.intesys.spring.interceptor.MyInterceptor" />
                	</mvc:interceptors>
                        ...
                with the namespace it works fine. Thanks a lot again.

                Enrico

                Comment

                Working...
                X