Spring's processor mapping (handler mapping) mechanism includes a truncation (handler interceptors), which is used to add specific processing to the request.
The Handlerinterceptor interface in the Org.springframework.web.servlet package must be implemented by the truncated device. The Handlerinterceptor interface defines three methods: Prehandle (..) method to run before the processor; Posthandle (..) The method runs after the processor; Aftercompletion (..) Method runs after the entire request has been processed.
These three methods greatly improve the flexibility of handling request.
Prehandle (..) Method returns a Boolean value that can be used to stop or continue the process. If this method returns true, the processing continues, and if this method returns false,dispatcherservlet that the truncation takes over the processing request (for example, rendering a view), no other truncation and processor will be executed.
An example of a truncated device:
Configure <mvc:interceptors> in Applicationcontext-mvc.xml (name may not be the same, but functionally);:
1 <mvc:interceptors>2 <Mvc:interceptor>3 <mvc:mappingPath="/**" />4 <mvc:exclude-mappingPath= "/test_receive_deal_audit_result" /> 5 <Beanclass= "samples." Timebasedaccessinterceptor ">6 < Propertyname= "Openingtime"value= "9"/>7 < Propertyname= "Closingtime"value= "+"/>8 </Bean>9 Ten </Mvc:interceptor> One </mvc:interceptors>
1 Package samples;2 3 Public class Timebasedaccessinterceptor extends Handlerinterceptoradapter {4 5 private int openingtime;6 private int closingtime;7 8 Public void Setopeningtime (int openingtime) {9 this.openingtime = openingtime;Ten } One A Public void Setclosingtime (int closingtime) { - this.closingtime = closingtime; - } the - 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; A } at response.sendredirect ("http://host.com/outsideOfficeHours.html"); - return false; - } - }
If the annotation driver is defined in the project, all request will be truncated by Timebasedaccessinterceptor. If the current time is outside of office hours, the user is redirected to a static page, and a static page reminds you to visit the site during office hours.
Reference: Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor
Finish
Truncate request with Handlerinterceptor