Spring Official document Translation--15.4 processor mapping (Handler mappings)

Source: Internet
Author: User

15.4 Processor Mapping (Handler mappings) Note: The following translates Handler into a processor

In spring's earlier version number, the user also had to define the processor mappings in the context of the Web application to configure the relationship between the request (requests) and the processor. In Spring2.5, Dispatcherservlet implements a default processor-mapped annotation implementation--defaultannotationhandlermapping, It can look for @requestmapping and @controllers annotations (translator Note: Need to configure Component-scan). In general, you do not need to override this default mapping, unless you need to define some default property values yourself.

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. That is, when the processor map cannot find a matching result.

Order

Spring sorts all available (available) processor mappings in the context according to the order value (see the Org.springframework.core.Ordered interface). Then match sequentially to the first satisfied handler

Alwaysusefullpath

assumed to be true. Spring will use the full path in the current servlet context to find a suitable processor. I feel false. The current servlet's path is then 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. Suppose you prefer a more encoded path and set it to false.

However, HttpServletRequest always exposes the servlet path to the decoded form.

You know, when compared to the encoded path. The servlet path does not match it.

Lazyinithandlers

Agree to lazy loading of a single processor (prototype processor is always lazy loading). 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 in handler mapping are required to implement the Org.springframework.web.servlet.HandlerInterceptor interface.

This interface defines three methods: The Prehandle method is run before the processor handle is run, and the Posthandle method is run after the processor handle method runs. A Aftercompletion method is followed by the completion of the complete request. That is, after rendering the view is run (Translator Note: The general simple need to use org.springframework.web.servlet.handler.HandlerInterceptorAdapter, to achieve pre-only/ Post-only on the line. These three methods provide a wide range of pre-and post-processing options that are flexible enough.

The Prehandle method returns a Boolean value.

Returns true, the processor run chain will continue to run if it returns false. Then Dispatcherservlet will feel that the interceptor itself has hosted the request (for example. The interceptor renders a suitable view for it, instead of continuing to run other interceptors and the current chain of operations 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. A request to get access from 9 o'clock in the morning to 6 o'clock in the afternoon was redirected to a 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. Suppose 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, for example, you can only visit the site during working hours.

As you can see, Spring adapter class Handlerinterceptoradapter simplifies the work of implementing the Handlerinterceptor interface directly.


If there is inaccurate translation of the place. I hope to correct my change ~

Spring Official document Translation--15.4 processor mapping (Handler mappings)

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.