码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC-数据绑定流程

时间:2016-02-28 11:07:42      阅读:652      评论:0      收藏:0      [点我收藏+]

标签:

1.Spring MVC主框架将ServletRequest对象以及目标方法的入参实例传递给WebDataBinderFactory实例,以创建DataBinder(数据绑定器)实例对象

2.DataBinder在调用装配在Srping MVC上下文中的conversionService

组件进行数据类型转换,数据格式化工作,将Servlet中的请求信息填充到入参对象

3.调用validator对已经绑定了请求消息的入参对象进行数据合法性校验,并最终生成数据绑定结果BindingData对象

4.SpringMVC抽取BindingResult中的入参对象和校验错误对象.将他们赋给处理方法的响应入参

调试

在实体类内类似this.name = name的地方打个断点(之前请确认已经添加了源码包)

技术分享

然后输入表单然后提交,在debug里找108行的地方

技术分享

    WebDataBinder binder = binderFactory.createBinder(request, attribute, name);

第一个参数是request,

第二个

技术分享

是product对象(这里的product是表单对象)

第三个就是这个类名的小写

     bindRequestParameters(binder, request);

//数据绑定,数据类型转换,数据格式化在这里执行
            validateIfApplicable(binder, parameter);

//数据验证

看一下variables

然后遭到binder

然后找到里面的conversionService里面有一个DefaultFormattinConversionService,负责数据转换和格式化

技术分享

技术分享

binder下面还有一个vallidator.

技术分享

如果在类型转换和格式化转换或者在validator中出错那么错误信息就会被保存在bindingResult

技术分享

 

下面自己看看conversionService

ConversionService converters =
    @org.springframework.format.annotation.DateTimeFormat java.lang.Long -> java.lang.String: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@617cbe00,@org.springframework.format.annotation.NumberFormat java.lang.Long -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.time.LocalDate -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@52594b7e
    @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.time.LocalDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@636493d9
    @org.springframework.format.annotation.DateTimeFormat java.time.LocalTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.time.LocalTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@608b71e
    @org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.time.OffsetDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@55ee403b
    @org.springframework.format.annotation.DateTimeFormat java.time.OffsetTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.time.OffsetTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@9a00ace
    @org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.time.ZonedDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@180c983c
    @org.springframework.format.annotation.DateTimeFormat java.util.Calendar -> java.lang.String: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@617cbe00
    @org.springframework.format.annotation.NumberFormat java.lang.Double -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    @org.springframework.format.annotation.NumberFormat java.lang.Float -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    @org.springframework.format.annotation.NumberFormat java.lang.Integer -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    @org.springframework.format.annotation.NumberFormat java.lang.Short -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    @org.springframework.format.annotation.NumberFormat java.math.BigDecimal -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    @org.springframework.format.annotation.NumberFormat java.math.BigInteger -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    java.lang.Boolean -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@1bd71e51
    java.lang.Character -> java.lang.Number : org.springframework.core.convert.support.CharacterToNumberFactory@6b1766ea
    java.lang.Character -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@66016b57
    java.lang.Enum -> java.lang.String : org.springframework.core.convert.support.EnumToStringConverter@3557024b
    java.lang.Long -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$LongToInstantConverter@19fa9943
    java.lang.Long -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter@c848827,java.lang.Long -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter@1050e0b
    java.lang.Long -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter@249ac564,java.lang.Long -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter@79652f80
    java.lang.Number -> java.lang.Character : org.springframework.core.convert.support.NumberToCharacterConverter@4e961303
    java.lang.Number -> java.lang.Number : org.springframework.core.convert.support.NumberToNumberConverterFactory@4448247f
    java.lang.Number -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@1ba7d073
    java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.lang.Long: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@617cbe00,java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Long: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.lang.String -> java.time.LocalDate: org.springframework.format.datetime.standard.TemporalAccessorParser@1fa4e0f7
    java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.lang.String -> java.time.LocalDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@5bff741a
    java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.lang.String -> java.time.LocalTime: org.springframework.format.datetime.standard.TemporalAccessorParser@7b7e3573
    java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.lang.String -> java.time.OffsetDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@7591b138
    java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.OffsetTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.lang.String -> java.time.OffsetTime: org.springframework.format.datetime.standard.TemporalAccessorParser@4e1bcf2
    java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@4c8621a5,java.lang.String -> java.time.ZonedDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@12d3f3e3
    java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.util.Calendar: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@617cbe00
    java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Double: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Float: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Short: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    java.lang.String -> @org.springframework.format.annotation.NumberFormat java.math.BigDecimal: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    java.lang.String -> @org.springframework.format.annotation.NumberFormat java.math.BigInteger: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@732dc57b
    java.lang.String -> java.lang.Boolean : org.springframework.core.convert.support.StringToBooleanConverter@76e19809
    java.lang.String -> java.lang.Character : org.springframework.core.convert.support.StringToCharacterConverter@636fd099
    java.lang.String -> java.lang.Enum : org.springframework.core.convert.support.StringToEnumConverterFactory@65a71823
    java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@25a1b3df
    java.lang.String -> java.time.Instant: org.springframework.format.datetime.standard.InstantFormatter@2329dbcd
    java.lang.String -> java.util.Date: app07a.formatter.DateFormatter@431c7516,java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.util.Date: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@617cbe00
    java.lang.String -> java.util.Locale : org.springframework.core.convert.support.StringToLocaleConverter@4f0ce538
    java.lang.String -> java.util.Properties : org.springframework.core.convert.support.StringToPropertiesConverter@31b124ac
    java.lang.String -> java.util.UUID : org.springframework.core.convert.support.StringToUUIDConverter@14743a09
    java.time.Instant -> java.lang.Long : org.springframework.format.datetime.standard.DateTimeConverters$InstantToLongConverter@49ba01fc
    java.time.Instant -> java.lang.String : org.springframework.format.datetime.standard.InstantFormatter@2329dbcd
    java.time.LocalDateTime -> java.time.LocalDate : org.springframework.format.datetime.sta...

找到StringToNumberConverterFactory

然后ctrl+shift+t,然后在45行打一个断点,然后重新执行,然后再次提交,"断点是用108行开始向下执行的,"先执行了实体类中的,然后执行了StringToNumberConverterFactory的断点

技术分享

 

springMVC-数据绑定流程

标签:

原文地址:http://www.cnblogs.com/rocky-AGE-24/p/5224205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!