Truncate request with Handlerinterceptor

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.