四:RequestMapping(二)

本文详细介绍了Spring MVC框架处理HTTP请求的过程,从DispatcherServlet的作用开始,解析如何通过URL映射定位到具体的Handler方法,再到参数解析及绑定的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()为对象赋值。

 


    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值