Example for using Spring Framework Interceptor for do something before go to Controller
Interceptor Class
package com.en.interceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.en.model.MasterModel;
public class MasterInterceptor extends HandlerInterceptorAdapter{
private String errorURL;
private MasterModel model;
public String getErrorURL() {
return errorURL;
}
public void setErrorURL(String errorURL) {
this.errorURL = errorURL;
}
public MasterModel getModel() {
return model;
}
public void setModel(MasterModel model) {
this.model = model;
}
}
package com.en.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class ExampleInterceptor extends MasterInterceptor{
private static Logger logger = Logger.getLogger(ExampleInterceptor.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//request.getRequestDispatcher(errorURL).forward(request, response);
//return false;
logger.info("Interceptor running");
return true;
}
}
Spring Configuration is the same for Spring Bean config
Config to URL Mapping that need to use interseptor
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref local="exampleInterceptor"/>
</list>
</property>
<property name="mappings">
<props>
<prop key="/example_insert.html">exampleController</prop>
<prop key="/example_load.html">exampleController</prop>
<prop key="/example_delete.html">exampleController</prop>
<prop key="/example_ws.html">exampleController</prop>
<prop key="/example_sap.html">exampleController</prop>
</props>
</property>
</bean>