Handler Interceptors For Annotation Based Configuration in Spring

Here’s what you need to have in your *-servlet.xml file:

<context:annotation-config />
<context:component-scan base-package="com.travelfusion.tfweb.guide.controller" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="interceptors">
      <ref bean="myInterceptor" />
   </property>
</bean>

<bean id="myInterceptor" class="com.company.MyInterceptor">
  <property name="openingTime"><value>9</value></property>
  <property name="closingTime"><value>18</value></property>
</bean>

And your interceptor class something like:

public class MyInterceptor implements HandlerInterceptor {
   private int openingTime;
   private int closingTime;

   @Override
   public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws    Exception {
      // TODO Auto-generated method stub
   }

   @Override
   public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws    Exception {
      // TODO Auto-generated method stub
   }

   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(HOUR_OF_DAY);
        if (openingTime <= hour < closingTime) {
           return true;
        } else {
           response.sendRedirect("http://host.com/outsideOfficeHours.html");
           return false;
        }
   }
}
Advertisements

It’s been so long, again!

It’s quiet crazy how time goes so fast! I haven’t had any time to make posts here, but I hope to write some more in the near future, and if not here at least on Travelfusion’s blog (I’ll post the link here when we go live). I’ve been working for Travelfusion since July and it’s been a great experience! If you want to find cheap flights you should check our website. I am currently the lead developer for the site and we have a planned a major redesign, so there are so many cool things to implement using GWT. Drop by anytime and give us some feedback.

I am loving London, so much to do, never bored – well almost never – and visiting friends and family once a month just makes up for the occasionally lonely days.

Here are some pictures of a team building event. I can’t say for sure that shooting each other is team building but it was a lot of fun.

CIMG2141