Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:26:58+00:00 2026-06-10T06:26:58+00:00

I’m trying to register a HandlerInterceptorAdapter instance though no matter what I do it

  • 0

I’m trying to register a HandlerInterceptorAdapter instance though no matter what I do it doesn’t get executed.

I’ve tried copying the example from the spring documentation with no luck.

Could it be that <annotation-driven> is doing something that could be preventing my interceptor from being registered?

I tried removing the id from handlerMapping though that only caused spring to register two instances in the container.

I’ve tried registering in both the root context and the servlet context.

Full servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

  <!-- Configures the @Controller programming model -->
  <annotation-driven/>

  <beans:bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <beans:property name="interceptors">
      <beans:list>
        <beans:ref bean="officeHoursInterceptor"/>
      </beans:list>
    </beans:property>
  </beans:bean>
  <beans:bean id="officeHoursInterceptor"
        class="com.johnsands.web.servlet.TimeBasedAccessInterceptor">
    <beans:property name="openingTime" value="9"/>
    <beans:property name="closingTime" value="18"/>
  </beans:bean>

  <!-- ReST Exception Handling
       http://www.stormpath.com/blog/spring-mvc-rest-exception-handling-best-practices-part-1
       -->
  <beans:bean id="restExceptionResolver"
              class="com.stormpath.spring.web.servlet.handler.RestExceptionHandler">
    <beans:property name="order" value="100"/>
    <beans:property name="errorConverter">
      <beans:bean class="com.johnsands.spring.web.servlet.handler.BasicRestErrorConverter"/>
    </beans:property>
    <beans:property name="errorResolver">
      <beans:bean class="com.stormpath.spring.web.servlet.handler.DefaultRestErrorResolver">
        <beans:property name="localeResolver" ref="localeResolver"/>
        <beans:property name="defaultMoreInfoUrl" value="mailto:support@mycompany.com"/>
        <beans:property name="exceptionMappingDefinitions">
          <beans:map>
            <!-- 404 -->
            <beans:entry key="com.johnsands.api.ResourceNotFoundException" value="404, _exmsg"/>

            <!-- 500 (catch all): -->
            <beans:entry key="Throwable" value="500"/>
          </beans:map>
        </beans:property>
      </beans:bean>
    </beans:property>
  </beans:bean>

  <beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/>

  <!-- Handles HTTP GET requests for /static/** by efficiently serving up static resources in the ${webappRoot}/static/ directory -->
  <resources mapping="/static/**" location="/static/" />

  <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
  <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>

  <!-- Imports user-defined @Controller beans that process client requests -->
  <beans:import resource="controllers.xml" />

</beans:beans>

controllers.xml doesn’t do much

<!-- Maps '/' requests to the 'home' view -->
<mvc:view-controller path="/" view-name="home"/>
<mvc:view-controller path="/login" view-name="login" />
<mvc:view-controller path="/logout" view-name="logout" />

<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.johnsands" />

Handler is as follows:

public class TimeBasedAccessInterceptor
        extends HandlerInterceptorAdapter {

    private static final Logger log =
            LoggerFactory.getLogger(TimeBasedAccessInterceptor.class);
    private int openingTime;
    private int closingTime;

    public TimeBasedAccessInterceptor() {
        log.info(" *** Constructing Instance *** ");
    }

    public void setOpeningTime(int openingTime) {
        this.openingTime = openingTime;
    }

    public void setClosingTime(int closingTime) {
        this.closingTime = closingTime;
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        log.info(" *** (preHandle) *** ");
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (openingTime <= hour && hour < closingTime) {
            return true;
        } else {
            response.sendRedirect("http://www.google.com.au");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object handler,
                           ModelAndView modelAndView)
            throws Exception {
        log.info(" *** (postHandle) *** ");
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler,
                                Exception ex)
            throws Exception {
        log.info(" *** (afterCompletion) *** ");
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:27:02+00:00Added an answer on June 10, 2026 at 6:27 am

    Yes, you are right about <mvc:annotation-driven/> overriding the RequestMappingHandlerMapping that you have created with the custom interceptor registered through it. The way to register the interceptor is instead with a <mvc:interceptors..>:

    <mvc:interceptors>
        <bean class=".."/>
    </mvc:interceptors>
    

    Or if you want it at a specific mapping:

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/test"/>
            <bean class="test"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    

    If you want an explicit RequestMappingHandlerMapping, then you will also need to define the HandlerAdapter to go with it and remove mvc:annotation-driven

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.