Results 1 to 8 of 8

Thread: Interceptor doesn't work

Hybrid View

  1. #1
    Join Date
    Jul 2009
    Posts
    10

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2009
    Posts
    10

    Default

    Hi Marten, thx a lot 4 your help,

    Quote 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?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    As I mentioned earlier post your configuration...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jul 2009
    Posts
    10

    Default

    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";
    		
    	}
    
    }

  6. #6
    Join Date
    Jul 2009
    Posts
    10

    Default

    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;
    	}
    
    }

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You use mvc:annotation-driven which overrides your configuration... I suggest a read of the reference guide how to register interceptors with the namespace...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •