Spring MVC 学习笔记 data binding

来源:互联网 发布:单片机工程师 编辑:程序博客网 时间:2024/06/06 01:13

Servlet中的输入参数为都是string类型,而spring mvc通过data bind机制将这些string 类型的输入参数转换为相应的command object(根据view和controller之间传输数据的具体逻辑,也可称为model attributes, domain model objects)。在这个转换过程中,spring实际是先利用java.beans.PropertyEditor中的 setAdText方法来把string格式的输入转换为bean属性, 亦可通过继承java.beans.PropertyEditorSupport来实现自定义的PropertyEditors,具体实现方式可参考spring reference 3.0.5 第 5.4节中的 Registering additional custom PropertyEditors部分。 自定义完毕propertyEditor后,有以下几种方式来注册自定义的customer propertyEditor.

 1:直接将自定义的propertyEditor放到需要处理的java bean相同的目录下

名称和java Bean相同但后面带Editor后缀。 例如需要转换的java bean 名为User,则在相同的包中存在UserEditor类可实现customer propertyEditor的自动注册。

2:利用@InitBinder来注册customer propertyEditor

这个在之前的笔记中已经介绍过了,即在controller类中增加一个使用@InitBinder标注的方法,在其中注册customer Editor

复制代码
    @InitBinder    public void initBinder(WebDataBinder binder) {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");        dateFormat.setLenient(false);        binder.registerCustomEditor(Date.class, new CustomDateEditor(                dateFormat, false));    }
复制代码

3:继承 WebBindingInitializer 接口来实现全局注册 使用@InitBinder只能对特定的controller类生效,为注册一个全局的customer Editor,可以实现接口WebBindingInitializer 。

复制代码
public class CustomerBinding implements WebBindingInitializer {    @Override    public void initBinder(WebDataBinder binder, WebRequest request) {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");        dateFormat.setLenient(false);        binder.registerCustomEditor(Date.class, new CustomDateEditor(                dateFormat, false));    }
复制代码

并修改 servlet context xml配置文件

复制代码
<bean        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        <property name="webBindingInitializer">            <bean                class="net.zhepu.web.customerBinding.CustomerBinding" />        </property>    </bean>
复制代码

但这样一来就无法使用mvc:annotation-driven  了。
使用conversion-service来注册自定义的converter DataBinder实现了PropertyEditorRegistry, TypeConverter这两个interface,而在spring mvc实际处理时,返回值都是return binder.convertIfNecessary(见HandlerMethodInvoker中的具体处理逻辑)。因此可以使用customer conversionService来实现自定义的类型转换。

复制代码
        <bean id="conversionService"        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <property name="converters">            <list>                <bean class="net.zhepu.web.customerBinding.CustomerConverter" />            </list>        </property>            </bean>
复制代码

 

需要修改spring service context xml配置文件中的annotation-driven,增加属性conversion-service指向新增的conversionService bean。

    <mvc:annotation-driven validator="validator"        conversion-service="conversionService" />

实际自定义的converter如下。

复制代码
 public class CustomerConverter implements Converter<String, Date> {    @Override    public Date convert(String source) {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");        dateFormat.setLenient(false);        try {            return dateFormat.parse(source);        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                return null;    }
复制代码

对于requestBody或httpEntity中数据的类型转换 Spring MVC中对于requestBody中发送的数据转换不是通过databind来实现,而是使用HttpMessageConverter来实现具体的类型转换。 例如,之前提到的json格式的输入,在将json格式的输入转换为具体的model的过程中,spring mvc首先找出request header中的contenttype,再遍历当前所注册的所有的HttpMessageConverter子类, 根据子类中的canRead()方法来决定调用哪个具体的子类来实现对requestBody中的数据的解析。如果当前所注册的httpMessageConverter中都无法解析对应contexttype类型,则抛出HttpMediaTypeNotSupportedException (http 415错误)。 那么需要如何注册自定义的messageConverter呢,很不幸,在spring 3.0.5中如果使用annotation-driven的配置方式的话,无法实现自定义的messageConverter的配置,必须老老实实的自己定义AnnotationMethodHandlerAdapter的bean定义,再设置其messageConverters以注册自定义的messageConverter。 在3.1版本中,将增加annotation-driven对自定义的messageConverter的支持 (SPR-7504),具体格式如下

 

复制代码
<mvc:annotation-driven>    <mvc:message-converters>        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>        <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>    </mvc:message-converters></mvc:annotation-driven>
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 军人保障卡丢失了怎么办 军人保障卡掉了怎么办 椎基底动脉供血不足怎么办 颈椎压迫神经脑供血不足怎么办 脑部基底动脉轻度狭窄怎么办 胸壁疼痛我该怎么办 厂房面积小于泄压面积怎么办 江苏海门农村自建房房产证怎么办 颈总动脉斑块形成怎么办 颈椎引起腔梗头晕怎么办 魅族手机变成英文怎么办 员工失去了工作乐趣怎么办 古墓丽影9出bug怎么办 气炉子打不着火怎么办 下面人员不参加公司拓展怎么办 重点班的差生怎么办 江苏考生选修考d怎么办 眼睛里有虫子该怎么办 屋里毛絮特别多怎么办 信用卡人员核核查单位地址怎么办 红米note电源键失灵怎么办 红米note4x跳屏怎么办 红米开机键坏了怎么办 红米note2开关键不灵敏怎么办 大学生论文检测系统忘记密码怎么办 快手实验室没有k歌怎么办 皮肤一到冬天就会起皮脱落怎么办 嘴上的皮撕破了怎么办 身体皮肤干燥起皮怎么办 把公司数据库弄宕机了怎么办 地铁上临时变站怎么办 大学实践课挂了怎么办 军训的人长得矮怎么办 军训脸被晒黑了怎么办 晚上吃了油炸的怎么办 楼梯又陡又窄怎么办 小孩不认真做作业怎么办 水厂出水水质不达标怎么办 野猪铁夹子夹到怎么办 燃气灶开孔尺寸大了怎么办 苹果电脑视频显示缺少插件怎么办