use of Spring handlerinterceptorTags: springexceptionobjectworkflowstringbean2007-05-25 16:56 21133 People read comments (0) favorite reports Spring (+)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Handlerinterceptor is a spring interceptor, which is particularly useful in some functional applications:
1. User Login and user Rights Management (see http://www.ideawu.net/ideablog/category4/article174.html)
2. According to the user's choice to decide whether to use HTML or Excel as the view (the application will be explained later)
3. Blackboard Building Block Application: Create a context before each controller, release context after render view
The Handlerinterceptor interface has several important methods:
Prehandleaction (actionrequest request, actionresponse response, Object handler):
This method is called before the controller's method is executed and can be used to break or continue the processing of the chain, and when returned true
, the processing execution chain continues, and when returned false
, does not execute the controller's method. (Verifying that the user is logged in is the best example of using the Prehandleaction method)
Afteractioncompletion (actionrequest request, actionresponse response, Object handler, Exception ex)
Prehandlerender (renderrequest request, renderresponse response, Object handler)
Posthandlerender (renderrequest request, renderresponse response, Object handler, Modelandview Modelandview)
These 3 methods are executed after the controller's method executes, before the Dispatcherservlet class is directed to view for render. The most useful thing is to use the Posthandlerender method, because it has modelandview in, so we can add an extra model object to the view before the render view. Or make changes to the view's Place ( For example, the "Use HTML or Excel as a view" example to change the view )
Afterrendercompletion (renderrequest request, renderresponse response, Object handler, Exception ex)
The method executes after the render view finishes, or after the request processing is complete. This method can be used to clean up resources (such as blackboard building block release context)
summarize the handlerinterceptor process :
- (
DispatcherServlet
maps a request to particular handler and assembles a handler execution chain consisting of the the handler that's to B e invoked and all of the HandlerInterceptor
instances, which apply to the request.)
preHandleAction(..)
is called; If the invocation of this method returns then this true
workflow continues
- The target handler handles the action phase of the request (via
HandlerAdapter.handleAction(..)
)
afterActionCompletion(..)
is called
preHandleRender(..)
is called; If the invocation of this method returns then this true
workflow continues
- The target handler handles the render phase of the request (via
HandlerAdapter.handleRender(..)
)
postHandleRender(..)
is called
afterRenderCompletion(..)
is called
We write our own interceptors can not directly implement Handlerinterceptor, but to extend the implementation of the Handlerinterceptor interface specific class Handlerinterceptoradapter, In this way we do not need to implement the above 5 methods, but only need to override the method we need to do it!
Here's an example of using an interceptor to implement the "Use HTML or Excel as a View" feature (only the key code is listed):
Search.html:
Search for:
<input type= "text" name= "Query" ><br>
Output in:
<input type= "Radio name="format"value=" xls "/>excel or
<input type= "Radio" name= "format" value= "html"/>html<br>
<input type= "Submit"/>
Searchcontroller.java:
Protected Modelandview handlerequestinternal ( httpservletrequest req, httpservletresponse response { String query = Requestutils.getrequiredstringparameter ("query"); List shows; Retrieve shows matching the query modelandview mav = new Modelandview ("Listshows", "shows", shows); return MAV;}
Outputformatmodificationinterceptor.java:
public class Outputformatmodificationinterceptor extends Handlerinterceptoradapter { private String parameter; Private String Defaultsuffix = ""; Private Properties suffices; Setters and getters omitted posthandler( httpservletrequest request, httpservletresponse response , modelandview Mav) throws Exception { String format = request.getparameter ("format"); String suffix = suffices.getproperty (format); Modify view name if exists if (suffix! = null) { mav.setviewname (mav.getviewname () + suffix); //Modify view to achieve the purpose of using different output formats } else { mav.setviewname (mav.getviewname () + Defaultsuffix);} }
Configure XML file:
<bean class= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" > <property name= " Interceptors "> <list><ref bean= "Formatinterceptor"/></list> </property></bean><bean id= "Formatinterceptor" class= "Org.springframework.prospring.web.OutputFormatModificationInterceptor "><property name= "suffices" ><props><entry key= "xls" >-xls</entry><entry key= "html" >-html</entry></props></property> <property name= "parameter" ><value>format</value></property><property name= "Defaultsuffix" ><value>-html</value></property></bean><bean name= "/listshows.action" class= "Org.springframework.prospring.web.controller.ListShowsController" > ... </bean> then you can use the following code in the Blackboard building block:
Public
classBbcontextinterceptor
extendsHandlerinterceptoradapter {
Public
Boolean Prehandle(HttpServletRequest request, httpservletresponse response, Object handler)
throwsException {Context CTX = Bbservicemanager.getcontextmanager (). SetContext (request);Request.setattribute (Requestattributekeyforbbcontext, CTX);Check user is a login or not are admin or not. If not, return false
return
true; }
Public
voidaftercompletion(HttpServletRequest request, httpservletresponse response, Object handler, Exception ex)
throwsException {Request.removeattribute (requestattributekeyforbbcontext);Bbservicemanager.getcontextmanager (). Purgecontext ();}}
Use of Spring Handlerinterceptor