views:

674

answers:

2

Hello,

I'm developping a web-app and i'm using Spring framework. Thing is, i never properly learned to use Spring and i'm kind of lost in all this.

However, using Annotations-based controllers, i was able to create most of my app!

Now, the problem is that i would need to be able ton intercept requests before they're sent to the controllers (i need it so i can validate the user has acces to the page he requests). I just spent about 5 hours searching for information about this and i actually found quite a lot, none of them worked as intended, i was never able to make my interceptor display a simple "Hello World".

Here's what i have in my *-servlet.xml (i also have the other beans definition of course):

<!-- this should be the class that contains the "hello world" -->
<bean id="myInterceptor" class="com.ibm.brmt.srb.admin.web.controller.TimeBasedAccessInterceptor"/>

<!-- this should "map" my interceptor no? -->
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
 <property name="interceptors">
  <list>
   <ref bean="myInterceptor"/>
  </list>
 </property>
</bean>

and here is my TimeBasedAccessInterceptor class (the name isn't relevant and probably will be changed)

package web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;



public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter{

    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
     System.out.println("-------------------------------------------------------------------------------------------");     
     }

    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
     System.out.println("-------------------------------------------------------------------------------------------");     
     }

    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
     System.out.println("-------------------------------------------------------------------------------------------");     
     return false;
    }
}

the codes compile and run but what's in the TimeBasedAccessInterceptor class is never called(i even used breakpoints). can someone help me?

as requested, here is a "preview" of the way i implements the controller in the *-servlet.xml

<bean id="controllerName" class="web.controller.controllerNameController"> 
 <property name="property1" ref="beanRef" />
 <property name="property1" ref="beanRef2"/>
</bean>

and in the controllerNameController.java:

  package web.controller;

    @Controller
    public class controllerNameController{

        @RequestMapping
        public void find(String[] enabledLvlCodes, String reset, String readRuleId, Filter filter, Errors errors,
          Model model, HttpSession session) {

       //Code goes here
       }
}
+1  A: 

Your mapping looks correct.

Perhaps you have other handler mappings defined in addition to DefaultAnnotationHandlerMapping (which would provide a way for your controller to be invoked via different path)?

Or perhaps your context file was not deployed properly (yes, that's a dumb suggestion but you'd be surprised how often this happens :-) )

It the answer to both is "NO", I'd suggest you put a breakpoint in AbstractHandlerMapping.getHandler() and step through to the point where HandlerExecutionChain is obtained and check whether it contains your interceptor.

ChssPly76
would: <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />be causing the problem?
Gadgetsan
absolutely. As skaffman mentioned in his comment, not having URI specified in @RequestMapping effectively renders your annotation mapping useless, causing fallback to ControllerClassNameHandlerMapping
ChssPly76
you're right, after removing the ControllerClassNameHandlerMapping and specifyin the URI in the controller, it did go into the interceptor, thanks a bunch you guys are amazing
Gadgetsan
in fact, if i just add the "interceptor" attribute to the ControllerClassNameHandlerMapping, i can keep my auto-mapping to the method name as i was using before
Gadgetsan
You certainly can, but then you should probably remove the `DefaultAnnotationHandlerMapping` and `@RequestMapping` annotations. Having two conflicting mapping schemes is not ideal.
ChssPly76
A: 

I actually opened account on Stack Overflow just to post this question. If you are using Spring why aren't you using Spring Security for configuring interceptors?

in the end, that's what we ended up doing
Gadgetsan