四:RequestMapping(二)

1.      在web配置中利用 DispatcherServlet作为所有请求参数的入口,doDispatch这个方法调用

// Determine handler for the current request.

mappedHandler=getHandler(processedRequest);

 

在此方法中获取到该请求路径对应的handlerMethod

2. 根据传入的路径,通过List<T>directPathMatches=this.urlMap.get(lookupPath);查找到对应的RequestMappingInfo

通过方法 PatternsRequestCondition.getMatchingCondition判断url是否匹配。

getMatchingPattern具体判断逻辑

 

matches.add(newMatch(match, this.handlerMethods.get(mapping)));

 

HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

 

// Actually invoke thehandler.

mv=ha.handle(processedRequest,response,mappedHandler.getHandler());

3.获取了HandlerAdapter后,Spring就会调用handlerAdapter实例的handle方法,并返回ModelAndView实例

 

来自 <https://my.oschina.net/HeliosFly/blog/214438>

4. 获取方法参数值。 

该方法是HandlerMethod中的方法,因此可以调用getMethodParameters()方法获取参数列表,然后遍历这些参数,分别用参数解析器来解析当前参数值,其中,argumentResolvers是HandlerMethodArgumentResolverComposite,概念讲解中已经阐述,它包含了所有的参数解析器的列表,以及参数类型和解析器的映射表,我们不妨看看到底什么怎么回事

Return the list of argument resolvers to useincluding built-in resolvers

RequestMappingHandlerAdapter.getDefaultArgumentResolvers()该方法注册了参数解析器。

5. 对于自定义的bean:ModelAttributeMethodProcessor.resolveArgument()最终在此方法中赋值操作this.bindRequestParameters(binder,request);

这里WebDataBinder方法bind中会使用BeanWrapper构造对象,然后设置对应的属性

 

TestModel tm = new TestModel(); BeanWrapper bw = new BeanWrapperImpl(tm);bw.setPropertyValue("good", "on");

good是boolean属性,使用BeanWrapperImpl设置属性的时候,内部会使用类型转换(父类TypeConverterSupport提供),将String类型转换为boolean,CustomBooleanEditor对于String值是on,1,true,yes都会转换为true,本文介绍PropertyEditorRegistrySupport的时候说明过,CustomBooleanEditor属于默认的属性编辑器。

 

来自 <http://www.cnblogs.com/fangjian0423/p/springMVC-databind-typeconvert.html>

6.      利用DataBinder.applyPropertyValues()为对象赋值。


    


猜你喜欢

转载自blog.csdn.net/lin5ting/article/details/80978145