SPRINGMVC Custom Interceptor Detailed (GO)

Source: Internet
Author: User

after a day of fighting, will finally use the SPRINGMVC, the following talk about his interceptor. Like Springmvc interceptors and Struts2 , Spring MVC can also use interceptors to intercept requests, and users can customize interceptors to implement specific functions. The custom interceptor must implement the Handlerinterceptor interface. Complete examples can be downloaded to my resources: http://download.csdn.net/download/tjcyjd/4255319

the code for the Handlerinterceptor interface is as follows:

Package org.springframework.web.servlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Public interface Handlerinterceptor {
The Prehandle () method is called before the business processor processes the request
Boolean Prehandle (HttpServletRequest request,
HttpServletResponse response,
Object handler)
Throws Exception;
The Posthandle () method is called after the business processor processes the request
void Posthandle (
HttpServletRequest request, HttpServletResponse
Response, Object
Handler, Modelandview Modelandview)
Throws Exception;
The Aftercompletion () method is called after Dispatcherservlet completely finishes processing the request
void Aftercompletion (
HttpServletRequest request, HttpServletResponse
Response, Object
Handler, Exception ex)
Throws Exception;

}

The following is an explanation of the three methods in the code.

Prehandle (): This method is called before the business processor processes the request, in which the user requests the request for processing. Returns true if the programmer decides that the interceptor will call another interceptor after intercepting the request, or if the programmer decides not to call another component to process the request, false.

Posthandle (): This method is called after the business processor finishes processing the request, but Dispatcherservlet returns the request to the client, in which the user requests the request for processing.

Aftercompletion (): This method is called after the Dispatcherservlet has completely processed the request, and some cleanup of resources can be done in the method.

Here's an example of how to use the SPRINGMVC framework interceptor.

Now you want to write an interceptor that intercepts all requests that are not working, forwards the requests to a specific static page, and does not handle their requests.

First write the Timeinterceptor.java, the code is as follows:

  1. Package com.yjde.web.interceptor;
  2. Import Java.util.Calendar;
  3. Import Javax.servlet.http.HttpServletRequest;
  4. Import Javax.servlet.http.HttpServletResponse;
  5. Import Org.springframework.web.servlet.ModelAndView;
  6. Import Org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  7. public class Timeinterceptor extends Handlerinterceptoradapter {
  8. Inheriting the Handlerinterceptoradapter class
  9. private int openingtime;//openingtime Property Specify office Hours
  10. private int closingtime;//Closingtime Property Specify off-hours
  11. Private String outsideofficehourspage;//Outsideofficehourspage property specifies the URL of the error prompt page
  12. Override the Prehandle () method to intercept the request before the business processor processes the request
  13. Public Boolean Prehandle (HttpServletRequest request,
  14. HttpServletResponse response, Object handler) throws Exception {
  15. Calendar cal = Calendar.getinstance ();
  16. int hour = Cal.get (calendar.hour_of_day);//Get current time
  17. if (Openingtime <= hour && Hour < Closingtime) {//Determine if currently in working time period
  18. return true;
  19. } Else {
  20. Response.sendredirect (Outsideofficehourspage); Back to the Tips page
  21. return false;
  22. }
  23. }
  24. }

    1. As you can see, the above code overloads the Prehandle () method, which is called before the business processor processes the request. In this method, the current time is first obtained to determine whether it is in the
    2. Between Openingtime and Closingtime, if True is returned, the business controller is called to process the request, otherwise it goes directly to a page and returns false so that the request is not processed.

Here is the configuration for the interceptor in Dispatcher-servlet.xml, with the following code:

  1. <!--
  2. Enables spring to support automatic detection of components, such as the controller of annotations
  3. -
  4. <context:component-scan base-package="Com.yjde.web.controller"/>
  5. <bean id="Viewresolver"
  6. class="Org.springframework.web.servlet.view.InternalResourceViewResolver"
  7. p:prefix="/web-inf/jsp/" p:suffix= ". jsp"/>
  8. <mvc:interceptors>
  9. <mvc:interceptor>
  10. <!--set the blocked path--
  11. <mvc:mapping path="/login1.htm"/>
  12. <mvc:mapping path="/login2.htm"/>
  13. <bean class="Com.yjde.web.interceptor.TimeInterceptor" >
  14. <!--openingtime Properties specify time-to-work
  15. <property name="Openingtime" >
  16. <value>12</value>
  17. </property>
  18. <!--closingtime Properties specify off-hours-
  19. <property name="Closingtime" >
  20. <value>14</value>
  21. </property>
  22. <!--Outsideofficehourspage property specifies the url--> of the prompt page
  23. <property name="Outsideofficehourspage" >
  24. <value>http://localhost:8080/springmvcinterceptor/jsp/outsideofficehours.jsp
  25. </value>
  26. </property>
  27. </bean>
  28. </mvc:interceptor>
  29. </mvc:interceptors>
  30. <bean id="Messagesource"
  31. class="Org.springframework.context.support.ResourceBundleMessageSource"
  32. P:basename="message" >
  33. </bean>

Original: http://blog.csdn.net/tjcyjd/article/details/7498236

SPRINGMVC Custom Interceptor Detailed (GO)

Related Article

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.