Sunday, April 20, 2014

[Spring] 15 - Spring MVC Handler Mapping

TOPIC
  • Available HandlerMapping Strategories
  • Chaining HandlerMappings
  • Handler Method Mapping Techniques
  • Resolving Handler Exceptions
  • Attaching Handle Interceptors

  • Available Handler Mapping Strategies
  • BeanNameUrlHandlerMapping

  • SimpleUrlHandlerMapping
* Notes:
  • Single component that has all of our mapping explicitly declared
  • Mapping table to map all mapping cases.
  • Controller-ClassNameHandlerMapping

  • You can also define your own

  • Chaining HandlerMappings
    • Multiple HandlerMapping strategies can be configured
      • For example, Simple, ControllerClass, BeanName
    • The DispatcherServlet will iterate (~iterate: looping to run) through them until a handler is found for the request
    • The iteration order can be configured


  • Handler Method Mapping Technique
    • Best practice often makes a single handler responsible for several related actions.
      • Especially for create-read-update-delete(CRUD) action
    • For example
      • A RewardController might create, show, update, and delete rewards.
    • Such multi-action handlers are typically mapped using wildcards
      • /reward/* or /reward/**
    • A common URL form is ${handler}/${action} like 
      • /reward/new
      • /reward/show
      • /reward/update
      • /reward/delete
    • The handler typically maps the requested ${action} to a method on itself

  • Multi Action Handlers with Spring MVC
    • To implement a multi-action handler in Spring MVC
      • Subclass MultiActionController
      • Configure the action method mapping strategy if desired
      • Implement your action methods
      • Register the Controller with the DispatcherServlet
* Notes:
MultiActionController is a standard MVC controller, and the RewardController is a subclass which extends the superclass MultiActionController
All of these methods are exactly the same as what you think inside of them, and they serve a certain set of URLs

  • MethodNameResolver Strategies
    • InternalPathMethodNameResolver(default)
      • As shown in previous example
      • Also provides configurable prefix and suffix
    • ParameterMethodNameResolver
      • Resolves parameter value (or name) to method
    • PropertiesMethodNameResolver
      • Allows for externalized mappings
      • Supports wildcards
    • or implement your own custom strategy
  • Custom Action Method Mapping Example
    • Suppose the following action method mappings are desired
      • GET /controller/new --> newForm
      • POST / controller/new -->create
      • GET / contorller/show/{id} --> show
    • To apply custom method mappings, configure a custom MethodNameResolver on your MultiActionController


  • Resolving Handler Exceptions
    • By default exceptions thrown by handlers propagate
      • The user will see a stack trace in their browser
    • Resolve exceptions to error views by registering a HandlerExceptonResolver with the DispatchServlet


  • Attaching Handler Interceptors
    • Specific groups of handlers can have their execution intercepted
      • Allows for applying pre and post-processing aspects
    • Especially useful for
      • Authorization checks
      • Centralized handling of application exceptions
      • Factoring out common handler code
  • Implementing a HandlerInterceptor
    • Implement the HandlerInterceptor interface
      • Consider extending from HandlerInteceptorAdapter and overriding the callbacks you need
    • Register your interceptor with the HandlerMapping for the handlers you wish to intercept
  • HandlerInterceptor vs. Filters
    • Both are "interceptors"
    • Handler Interceptors are finer-grained
      • Filters intercept Servlets
    • Handler Interceptors are best suited for factoring out common handler code
      • Filters are best suited for more general processing
    • Handler Interceptors are managed by Spring
      • Filters are managed by the Servlet container
  • DelegatingFilterProxy
    • DelegatingFilterProxy is a Servlet Filter which delegates to a Filter managed by Spring
    • Allows a Filter to benefit from dependency injection
    • How to use
      • Define the "real" Filter as a Spring bean
      • Define Spring's DelegatingFilterProxy in web.xml
        • Have it reference your Filter bean by its name


  • Summary
  • Spring MVC supports several ways to map requests to Handlers
    • Bean Name, Explicit (Simple), Convention-based
  • Use MultiActionController to have a single handler centralize several related actions
    • An AccountController to create, edit, delete and sow accounts
  • Use a HandlerExceptionResolver to map application exceptions to error views
  • Use HandlerInterceptors and Filters to intercept request processing and factor out common handler code

No comments:

Post a Comment