Spring MVC 之 HandlerMapping

The role of HanderMapping 

The HandlerMapping class defines the mapping between requests and handlers. Finding the handler corresponding to the request is the first step for SpringMVC DispatcherServlet to process the request. Find the corresponding handler object handlerExecutionChain by analyzing the request url and message header headers, and then DispatcherServlet will execute the handler method handler method contained in the handlerExecutionChain and a series of pre- and post-method interceptors handler Interceptor.

public interface HandlerMapping {
      HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
}

Subclass of HandlerMapping

 

  • RequestMappingHandlerMapping

RequestMappingHandlerMapping - Create a RequestMappingInfo instance that defines the mapping relationship between request and handler method based on the class-level and method-level @RequestMapping annotations in the @Controller-annotated class. The basic usage is as follows:

@Controller
public class WelcomeController 
{
    @RequestMapping("/welcome")
    protected ModelAndView someMethod() throws Exception {
        System.out.println("Inside WelcomeController");

        ModelAndView model = new ModelAndView("welcome");
        model.addObject("message", "JavaInterviewPoint");

        return model;
    }

}
  • BeanNameUrlHandlerMapping

BeanNameUrlHandlerMapping - Implementation class of the HandlerMapping interface for mapping from URLs to bean names starting with a slash /, similar to how URLs are mapped to action names in Struts. Its characteristic is that only one url-bean mapping relationship can be registered at a time. If multiple urls are mapped to a bean, multiple registrations are required. The basic usage is as follows:

//用法1,直接在@Controller注解上定义别名,注册映射/welcome->WelcomeController
@Controller("/welcome")
public class WelcomeController  extends AbstractController
{
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
                                                 HttpServletResponse response) throws Exception {
       
        ModelAndView model = new ModelAndView("welcome");
        model.addObject("message", "JavaInterviewPoint");

        return model;
    }
}
//用法2,在@Configuration注解类中定义指定别名的Bean,注册映射/welcome1、/welcome2->WelcomeController
@Configuration
public class BeanNameUrlHandlerMappingConfig {
    @Bean
    BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
        return new BeanNameUrlHandlerMapping();
    }
 
    @Bean({"/welcome1","/welcome2"})
    public WelcomeController welcome() {
        return new WelcomeController();
    }
}
  • SimpleUrlHandlerMapping

SimpleUrlHandlerMapping - Implementation class of HandlerMapping interface for mapping from URL to request handler bean. Supports mapping to bean instances as well as mapping to bean names.

SimpleUrlHandlerMapping is similar to BeanNameUrlHandlerMapping, both based on the url and bean mapping relationship, but SimpleUrlHandlerMapping is more flexible. Its setUrlMap method supports registering multiple Url mappings at one time.

//注册映射/welcome1、/welcome2->WelcomeController
@Configuration
public class UrlHandlerMappingConfig {
 @Bean
    SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
        Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
        urlMap.put("welcome1","welcomeController");
        urlMap.put("welcome2","welcomeController");
        SimpleUrlHandlerMapping handlerMapping=new SimpleUrlHandlerMapping();
        handlerMapping.setUrlMap(urlMap);
        return handlerMapping;
    }

    @Bean WelcomeController welcomeController(){
        return new WelcomeController();
    }
 
     
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325296457&siteId=291194637