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:
- Package com.yjde.web.interceptor;
- Import Java.util.Calendar;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- Import Org.springframework.web.servlet.ModelAndView;
- Import Org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
- public class Timeinterceptor extends Handlerinterceptoradapter {
- Inheriting the Handlerinterceptoradapter class
- private int openingtime;//openingtime Property Specify office Hours
- private int closingtime;//Closingtime Property Specify off-hours
- Private String outsideofficehourspage;//Outsideofficehourspage property specifies the URL of the error prompt page
- Override the Prehandle () method to intercept the request before the business processor processes the request
- Public Boolean Prehandle (HttpServletRequest request,
- HttpServletResponse response, Object handler) throws Exception {
- Calendar cal = Calendar.getinstance ();
- int hour = Cal.get (calendar.hour_of_day);//Get current time
- if (Openingtime <= hour && Hour < Closingtime) {//Determine if currently in working time period
- return true;
- } Else {
- Response.sendredirect (Outsideofficehourspage); Back to the Tips page
- return false;
- }
- }
- }
- 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
- 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:
- <!--
- Enables spring to support automatic detection of components, such as the controller of annotations
- -
- <context:component-scan base-package="Com.yjde.web.controller"/>
- <bean id="Viewresolver"
- class="Org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/web-inf/jsp/" p:suffix= ". jsp"/>
- <mvc:interceptors>
- <mvc:interceptor>
- <!--set the blocked path--
- <mvc:mapping path="/login1.htm"/>
- <mvc:mapping path="/login2.htm"/>
- <bean class="Com.yjde.web.interceptor.TimeInterceptor" >
- <!--openingtime Properties specify time-to-work
- <property name="Openingtime" >
- <value>12</value>
- </property>
- <!--closingtime Properties specify off-hours-
- <property name="Closingtime" >
- <value>14</value>
- </property>
- <!--Outsideofficehourspage property specifies the url--> of the prompt page
- <property name="Outsideofficehourspage" >
- <value>http://localhost:8080/springmvcinterceptor/jsp/outsideofficehours.jsp
- </value>
- </property>
- </bean>
- </mvc:interceptor>
- </mvc:interceptors>
- <bean id="Messagesource"
- class="Org.springframework.context.support.ResourceBundleMessageSource"
- P:basename="message" >
- </bean>
Original: http://blog.csdn.net/tjcyjd/article/details/7498236
SPRINGMVC Custom Interceptor Detailed (GO)