Home Spring Spring3.2 Requests Intercepting through a HandlerInterceptor



Requests Intercepting through a HandlerInterceptor
Posted on: January 9, 2013 at 12:00 AM
In this section, you will learn about requests interception through a HandlerInterceptor.

Requests Intercepting through a HandlerInterceptor

In this section, you will learn about requests interception through a HandlerInterceptor.

In Spring , special bean types are needed by DispatcherServlet to process the requests and render the suitable view back to the client. HandlerMapping is one of the special bean types that maps oncoming requests to handlers and handler interceptors according to some criteria. Given below sample configuration of a interceptor using HandlerMapping  bean :

<beans>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
	<property name="interceptors">
		<bean class="roseindia.MyInterceptor"/>
	</property>
</bean>

<beans>

HandlerInterceptor

When you want to implement a particular functionality to incoming requests, interceptors of handler mapping(HandlerMapping bean) is useful. The HandlerInterceptor of org.springframework.web.servlet package must be implemented by the Interceptors in handler mapping. This interceptor has three methods for all kinds of preprocessing and postprocessing , these are :

  • preHandle() : This method is invoked before handler

  • postHandle() : This method is invoked after handler execution.

  • afterCompletion() : This method is invoked after the request is finished.

Execution chain can be break or continue using preHandle() method. If preHandle() method returns true, means processing of the handler execution chain will continues. If it returns false, processing of the handler execution chain will break and the actual handler will not be executed.

Sample Code

Given below the sample configuration and code for an interceptor of handler mapping :

Configuration :

<beans>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="interceptors">
<list>
<ref bean="MyInterceptor"/>
</list>
</property>
</bean>

<bean id="MyInterceptor"
class="roseindia.InputInterceptor">
<property name="firstInput" value="9"/>
<property name="finalInput" value="18"/>
</bean>
<beans>

Code :

package roseindia;

public class InputInterceptor extends HandlerInterceptorAdapter {

private int firstInput;
private int finalInput;

public void setFirstInpu(int firstInput) {
this.firstInput = firstInput;
}

public void setFinalInput(int finalInput) {
this.finalInput = finalInput;
}

public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {

Calendar cal = Calendar.getInstance();
int day= cal.get(DAY_OF_MONTH);
if (firstInput <= day && day < finalInput) {
return true;
} else {
response.sendRedirect("http://roseindia.net/outofdate.html");
return false;
}
}
}

Related Tags for Requests Intercepting through a HandlerInterceptor:


More Tutorials from this section

Ask Questions?    Discuss: Requests Intercepting through a HandlerInterceptor  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.