15.4 Processor Mapping (Handler mappings) Note: The Handler is translated into a processor below
In earlier versions of spring, users also needed to define processor mappings in the context of a Web application to configure the relationship between requests (requests) and processors. In Spring2.5, Dispatcherservlet implements a default processor-mapped annotation implementation--defaultannotationhandlermapping, It is able to look for @requestmapping and @controllers annotations (translator Note: Need to configure Component-scan). In general, you do not need to override this default mapping relationship unless you need to customize some of the default property values. The properties are listed below:
Interceptors (Interceptor)
Configures a set of interceptors to use. Handlerinterceptors the "Intercepting Requests-the Handlerinterceptor Interface" section of this chapter has a further introduction.
Default Handler
Configure the default processor, which is used when the processor mapping cannot find a matching result.
Order
Spring sorts all the available (available) processor mappings in the context based on the order value (see the Org.springframework.core.Ordered interface). Then match sequentially to the first satisfied handler
Alwaysusefullpath
If true,spring will use the full path in the context of the current servlet to find a suitable processor. The default is False, then the current servlet's path is used as the base path. For example, a servlet uses testing/* to map, and the Alwaysusefullpath property is set to true, at which point the path used is/testing/viewpage.html. Conversely, if Alwaysusefullpath is false, then the path is/viewpage.html
UrlDecode
For Spring2.5, the default value is true. If you prefer to compare the encoded path, set it to false. However, HttpServletRequest always exposes the servlet path to the decoded form. Be aware that the servlet path does not match when compared to the encoded path.
Lazyinithandlers
Allows lazy loading of a single processor (the prototype processor is always lazy-loaded). The default value is False
Attention
Alwaysusefullpath, UrlDecode and Lazyinithandlers These properties are only valid for org.springframework.web.servlet.handler.AbstractUrlHandlerMapping subclasses
The following example shows how to override the default mapping and add an interceptor (Interceptor)
<beans><bean id= "handlermapping" class= " Org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping<property name= "Interceptors" ><bean class= "Example. Myinterceptor "/></property></bean><beans>
Intercept Request--handlerinterceptor Interface interceptingrequests-the Handlerinterceptor interface
Spring's processor mapping mechanism includes interceptors, which are useful when you want to apply some specific functionality to a specified requests, such as: authentication;
Interceptors located in the handler mapping must implement the Org.springframework.web.servlet.HandlerInterceptor interface. This interface defines three methods: The Prehandle method is executed before the processor handle executes, the Posthandle method is executed after the processor handle method executes, and the other Aftercompletion method is after the complete request is completed. That is, it is executed after rendering the view (Translator Note: General simple requirements use Org.springframework.web.servlet.handler.HandlerInterceptorAdapter to achieve pre-only/ Post-only on it. These three methods provide a wide range of pre-and post-processing options that are flexible enough to be used.
Where the Prehandle method returns a Boolean value. Returns TRUE, the processor execution chain will continue to execute, and if it returns false, then Dispatcherservlet will assume that the interceptor itself has managed the request (for example, the interceptor renders a suitable view for it). Instead of continuing to execute other interceptors and the current execution chain is terminated.
The following example defines a processor mapping that maps all matching URLs ("/*.form" or "/*.view") to the specified controller (controller--editaccountformcontroller). An interceptor was added to intercept these matching requests, allowing requests that were not accessed from 9 o'clock in the morning to 6 o'clock in the afternoon to be redirected to another page.
<beans><bean id= "handlermapping" class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping "><property name=" interceptors "><list><ref bean=" Officehoursinterceptor "/></list>< /property><property name= "Mappings" ><value>spring Framework3.0 Reference documentation 440/*.form= Editaccountformcontroller/*.view=editaccountformcontroller</value></property></bean><bean Id= "Officehoursinterceptor" class= "samples. Timebasedaccessinterceptor "><property name=" Openingtime "value=" 9 "/><property name=" ClosingTime "value = "/></bean><beans>"
Package Samples;public class Timebasedaccessinterceptor extends Handlerinterceptoradapter {private int openingtime; private int closingtime;public void setopeningtime (int openingtime) {this.openingtime = Openingtime;} public void setclosingtime (int closingtime) {this.closingtime = Closingtime;} 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 < Closingtime) {return true;} else {response.sendredirect ("http://host.com/outsideOfficeHours.html"); return false;}}}
Any requests that are matched by this mapping will be intercepted by Timebasedaccessinterceptor. If the current time is not in office hours (that is, 9 o'clock in the morning to 6 o'clock in the afternoon), then the user will be redirected to a static HTML page, such as you can only access the site during working hours.
As you can see, the spring adapter class Handlerinterceptoradapter simplifies the work of implementing the Handlerinterceptor interface directly.
If there is inaccurate translation, I hope to correct the change ~
Spring Official document Translation--15.4 processor mapping (Handler mappings)