Is this the relationship between HandlerMapping and HandlerAdapter in Spring mvc?

Spring mvc,urlurl

java
    handler;
    
Spring mvcHandlerMappingHandlerAdapter:
HandlerMapping(handler),
HandlerAdapter;


HandlerMapping is the mapping processor, and HandlerMapping will map the request to a HandlerExecutionChain object (including a Handler processor (page controller) object and multiple HandlerInterceptor interceptors). Through this policy mode, it is easy to add new mapping policies.

HandlerAdapter is the call of processor adaptation and processor function processing method. HandlerAdapter will call the real processor function processing method according to the adaptation result to complete the function processing; and return a ModelAndView object (including model data, logical view name);

generally speaking, HandlerMapping is responsible for finding a method of the controller object or only finding a controller object (depending on what mapping processor and processing adapter you configure in xml), while HandlerAdapter is responsible for calling the corresponding methods of the controller object.

The

example 1:HandlerMapping is responsible for finding a method for a controller object.

<bean   
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  
<bean  
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>  
@Controller
public class BlogController extends BaseController{
    
    @Autowired
    private CommentService commentService;
    
    @Autowired
    private BlogService blogService;
    
    
    
    @RequestMapping("/test")
    public String blog(Model model){
        return getFrontView("blog/blog");
    }
}

access request localhost:8080/test
if HandlerMapping just finds the object, the above example cannot find the BlogController controller object. It can actually be found, so you already know what method to call in HandlerMapping.

example 2:HandlerMapping is responsible for finding a controller object.

<!-- HandlerMapping -->  
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>  
   
<!-- HandlerAdapter -->  
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

<bean name="/hello" class="cn.javass.chapter2.web.controller.HelloWorldController"/>    
public class HelloWorldController implements Controller {  
    @Override  
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {  
       ModelAndView mv = new ModelAndView();  
       mv.addObject("message", "Hello World!");  
       mv.setViewName("hello");  
       return mv;  
    }  
}  

access request http://localhost:8080/hello
in this case, HandlerMapping simply finds the controller object, and then SimpleControllerHandlerAdapter executes the fixed handleRequest method ();

in the controller.

in principle, we should first find handler , then find adapter according to handler , and finally let adapter execute final method . Because of the complexity of Spring itself, I have never understood the routing block, but generally speaking, it is still a url corresponding to a method , which can be regarded as a route registration.

finally, I would like to talk about an irrelevant one, because the routing of spring is really too complicated. If you really want to see the routing, you can take a look at the routing design of blade . The principle is the same.


handlerMapping is understood correctly;
handler is actually working, and adapter adapts to different handler (interface adapter mode). Call handler for processing, which is convenient for unified processing.
handler can be any object,


not handlerMapping, but to find handler

Menu