Handler mappings

In previous versions of Spring,users were required to define one or more HandlerMapping beans in the web application context ot map incoming web requests to appropriate handlers.With the introduction of annotated controllers,you generally don’t need to do that because the RequestMappingHandlerMapping automatically looks for @RequestMapping annotations on all
@Controller beans.However,do keep in mind that all HandlerMapping classes extending rom AbstractHandlerMapping have the following properties that you can use to customize their behavior:
- interceptors List of interceptors to use.HandlerInterceptors are discussed in the section called “Intercepting requests with a HandlerInterceptor”.
- defaultHandler Default handler to use,when this handler mapping does not result in a matching handler.
- order Based on the value of the order property(see the org.springframework.core.Ordered interface),Spring sorts all handler mappings available in the context and applies the first matching handler.
- alwaysUseFullPath if true,Spring uses the full path within the current Servlet context to find an appropriate handler.If false(the default),the path within the current Servlet mapping is used.For example,if a Servlet is mapped using /testing/* and the alwaysUseFullPath property is set to true,/testing/viewPage.html is used,whereas if the property is set to false,/viewPage.html is used.

The following example shows how to configure an interceptor:

<beans>
    <bean id="handlerMapping" class="org.springfarmework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="interceptor">
        <bean class="example.MyInterceptor"/>
    </property>
    </bean>
</beans>

Intercepting request with a HandlerInterceptor

Spring’s handler mapping mechanism includes handler interceptors, which are useful when you want to apply specific functionality to certain requests, for example,checking for a principal.
Interceptors located in the handler mapping must implement HandlerInterceptor from the org.springframework.web.servlet package.This interface defines three methods:
preHandle(..)is called before the actual handler is executed;postHandler(..)is called after the handler is executed; and afterCompletion(..)is called after the complete request has finished.These three methods should provide enough flexibility to do all kinds of preprocessing and postprocessing.

The preHandle(..)method returns a boolean value.You can use this method to break or continue the processing of the execution chain.When this method returns true,the handler execution chain will continue; when it reurns false,the DispatcherServlet assumes the interceptor itself has taken care of requests and does not continue executing the other interceptors and the actual handler in the execution chain.

Interceptors can be configured using the interceptors property ,which is present on all HandlerMapping class es extending from AbstractHandlerMapping.This is shown in the example below:

<beans>
    <bean id="handlerMapping" class="org.springfarmework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="interceptor">
        <bean class="example.MyInterceptor"/>
    </property>
    </bean>
    <bean id="officeHoursInterceptor" class="samples.TimeBaseAccessInterceptor">
        <property name="openingTime" value="9"/>
        <property name="closingTime" value="18"/>
    </bean>
</beans>

Any request handled by this mapping is intercepted by the TimeBasedAccessInterceptor.If the current time is outside office hours,the user is redirected to static HTML file that says,for example,you can only access the website during office hours.

Note

When using the requestMappingHandlerMapping the actual handler is an instance of HandlerMethod which identifies the specific controller that will be invoked.

As you can see,the Spring adapter class HandlerInterceptorAdapter makes it easier to extend the HandlerInterceptor interface.

Tip

In the example above,the configured interceptor will apply to all requests handled with annotated controller methods.If you want to narrow down the URL paths to which an interceptor applies,you can use the MVC namespace or the MVC java config,or decalre bean instances of type MappedInterceptor to do taht .

Method Paramters And Type Conversion

String-based values extracted fro the request including request including request parameters,path variables,request headers,and cookie values may need to be converted to the target type of the method parameter or field they’re bound to.If the target type is not String,Spring automatically converts to the approprivte type.All simple types such as int,long,Date,etc.are supported.You can further customize the conversion process through a WebDataBinder(seee the section called “Customizing WebDataBinder initialization”) or by registering Foormatters with the FormattingConversionService(see Section 8.6, “Spring Field Formatting”)

Customizing WebDataBinder initialization

To customize request parameter binding with PropertyEditors,etc,via Spring’s WebDataBinder,you can either use @InitBinder-annotated methods with your controller or externalize your configuration by providing a custom WebBindingInitializer.

Customizing data binding with @InitBinder

Annotating controller methods with @InitBinder allows you to configure web data binding directly within your controller class.@InitBinder identifies methods which initalize the WebDataBinder which will be used for populatin command and form object arguments of annotated handler method.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章