I am beginner in Spring MVC. I didn't understand handler adapters clearly. What is a handler adapter and when do I use adapters?
5 Answers
A HandlerMapping maps a method to a URL, so the DispatcherServlet knows which method should be invoked by a specific request. Then the DispatcherServlet use a HandlerAdapter to invoke the method.
Why DispatcherServlet does not invoke a method directly?
Because there are many ways to invoke a method, like annotation, xml etc. HandlerAdapter de-couples the DispatcherServlet and the invoked actions.
Comments
This section of the Spring docs discusses the default adapters and how they relate to annotation configuration.
Briefly, handler adapters decide which controller (and method) to call for a request.
2 Comments
You can find Adapter in HandlerAdapter and that part of name comes from Adapter pattern. Adapter is like a bridge between two objects and HandlerAdapter is a bridge between handler object and dispatcher servlet.
As you can see from the HandlerAdapter source code below taken from Spring documentation, there is one method, handle method with ModelAndView return type. Every HandlerAdapter will implement this method to delegate the HttpServletRequest and HttpServletResponse to handler object so then handler object will execute application logic using these HttpServletRequest/Response.
public interface HandlerAdapter { //Check if controller is supported boolean supports(Object handler); //handle request ModelAndView handle(HttpServletRequest rqst, HttpServletResponse rsp, Object handler) throws Exception; This application logic execution produces model and view. The view can be in form of view name String or View object. The model holds data that will be used to render the view. HandlerAdapter will wrap the model and view in ModelAndView object. It is dispatcher servlet job to process ModelAndView object.
Dispatcher servlet does not know about handler object and relieved from directly handle application logic. Handler object also relieved from converting model and view into ModelAndView object because HandlerAdapter will do that converting job.
Comments
A HandlerMapping simply maps a method to a URL. Most beginners don't use this object directly, but rather use RequestMapping's instead. The return type of the mapped method (generally) determines what view SpringMVC will use to render a response.
For example, the following RequestMapping will generate a HandlerMapping for GET requests to "/" or "/home" to invoke this method:
@RequestMapping(value={"/", "/home"}, method=RequestMethod.GET) public String getHome() { return "homepage"; } The method returns a string name of a view, which would typically be resolved to "/WEB-INF/views/homepage.jsp" (but that depends on your ViewResolver of course)
Just an fyi for starting out: you can add different objects that you might need as parameters to your method (like Locale, HttpServletRequest, etc). See the RequestMapping javadoc for more info.
1 Comment
From the Spring docs
It follow SPI design pattern
MVC framework SPI interface, allowing parameterization of core MVC workflow.
It helps with integrating with other frameworks (because since we are not directly using handler object and using the adapter to invoke. This means the adapter takes care of other plumbing logic for using handler)
Note that a handler can be of type Object. This is to enable handlers from other frameworks to be integrated with this framework without custom coding, as well as to allow for annotation handler objects that do not obey any specific Java interface.
Importantly we should note that it is not for application developers:
This interface is not intended for application developers. It is available to handlers who want to develop their own web workflow.