Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Requests Intercepting through a HandlerInterceptor

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

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

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

Ads
Share on Google+Share on Google+

Requests Intercepting through a HandlerInterceptor

Posted on: January 9, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Requests Intercepting through a HandlerInterceptor  

Post your Comment


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

Ads

 

Ads