Home Spring Spring3.2 Requests Intercepting through a HandlerInterceptor
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Requests Intercepting through a HandlerInterceptor

Advertisement
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;
}
}
}
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 9, 2013

Related Tutorials

Ask Questions?    Discuss: Requests Intercepting through a HandlerInterceptor  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com