voldemar
voldemar

Reputation: 113

HandlerInterceptorAdapter doesn't work

Please help to a Spring MVC newbie. I'm having hard time to make HandlerInterceptorAdapter work. Here is the code and the configuration file. mvc-dispatcher-servlet.xml:

  <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      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
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven />

<context:component-scan base-package="com.myproject" />

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
    <property name="interceptors">
        <list>
            <ref bean="loginInterceptor" />
        </list>
    </property>
</bean>


<bean id="loginInterceptor" class="com.myproject.util.login.LoginInterceptor" />


<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

LoginInterceptor.java:

    package com.myproject.util.login;
    import org.apache.log4j.Logger;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class LoginInterceptor extends HandlerInterceptorAdapter {
    private static final Logger LOG = Logger.getLogger(LoginInterceptor.class);

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    LOG.debug("intercepted");
    return super.preHandle(request, response, handler);
  }
}

I'm trying to make this interceptor run for any controller. Perhaps, ControllerClassNameHandlerMapping, is not the best choice for this purpose. Please, help.

Upvotes: 2

Views: 6406

Answers (2)

Rori Stumpf
Rori Stumpf

Reputation: 1977

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/somepath/**" />
        <bean class="com.mydomain.myapp.SomeInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

I found that "mvc:interceptors" worked instead of XML beans - see: Spring MVC: Why does this <mvc:interceptors> declaration work, but not the traditional XML?

Upvotes: 4

woyaru
woyaru

Reputation: 5624

Try to use:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

Instead of:

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">

Upvotes: -1

Related Questions