Gadgetsan
Gadgetsan

Reputation: 67

Intercepting requests before they go to the controller in an annotation-based Spring web-app

I'm developing 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 access 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
       }
}

Upvotes: 3

Views: 5877

Answers (1)

ChssPly76
ChssPly76

Reputation: 100706

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.

Upvotes: 1

Related Questions