Spring's handler mapping mechanism provides a handler interceptors that can be used to add specific functionality to specific requests, such as the amount of money to be detected.
The handler mapping interceptor must implement the Handlerinterceptor interface. The interface defines three methods:
Prehandler ()-called Posthandler () before the actual handler is executed-is invoked after handler is executed aftercompletion ()-called after the request processing is completed
The Prehandler () method returns a Boolean value. You can use this method to continue or break the execution chain. Returns true, continues execution of the chain, or returns false, stopping subsequent execution.
Interceptor is configured using the Interceptors property, which is the property of the Handlermapping class.
For example:
<beans> <bean id= "handlermapping" class=
Thod.annotation.RequestMappingHandlerMapping "> <property name=" interceptors "> <list> <ref bean= "Officehoursinterceptor"/> </list> </property> </bean > <bean id= "officehoursinterceptor" class= "samples." Timebasedaccessinterceptor "> <property name=" openingtime "value=" 9 "/> <property name=" ClosingT IME "value="/> </bean> <beans>
Package samples;
public class Timebasedaccessinterceptor extends Handlerinterceptoradapter {
private int openingtime;
private int closingtime;
public void setopeningtime (int openingtime) {
this.openingtime = openingtime;
}
public void setclosingtime (int closingtime) {
this.closingtime = closingtime;
}
public boolean prehandle (HttpServletRequest request, httpservletresponse response,
Object handler) throws Exception {
Calendar cal = Calendar.getinstance ();
int hour = Cal.get (hour_of_day);
if (Openingtime <= hour && Hour < Closingtime) {return
true;
}
Response.sendredirect ("http://host.com/outsideOfficeHours.html");
return false;
}
}
Any request requested by the handler will be interrupted by Timebasedaccessinterceptor.
Use is the base class that configures the Handlerinterceptoradaptor class as an interrupt (the implementation class of the Handlerinterceptor interface).