SpringMVC学习笔记----带有复杂类型的command类
2014-12-11 22:23
330 查看
在学习BaseCommandController时,我们知道,当提交表单时,controller会把表单元素注入到command类里,但是系统注入的只能是基本类型,如int,char,String。但当我们在command类里需要复杂类型,
如Integer,date,或自己定义的类时,controller就不会那么聪明了。
一般的做法是在自己的controller里override initBinder()方法。这里我们修改一下学习SimpleFormController的那个例子。
覆盖initBinder()方法
在执行请求参数到Command对象绑定之前,初始化一个可用的DataBinder实例,就是有了一个ServletRequestDataBinder之后,我们可以添加自定义的PropertyEdtior
以支持某些特殊数据类型的数据的绑定,或者排除某些不想绑定的请求参数,这些定制行为可以通过覆写initBinder()方法引入
下面的例子是在之前的SimpleFormController基础上进行了一些小小的变动
Command类中的主要变动时增加了一个Date类型的birthday字段,get字段的类型由int变成了Integer,这些都是不能直接将请求参数绑定到Command对象上
在Controller里面覆盖initBinder()方法。
可以看出使用了binder.registerCustomEditor方法,它是用来注册的。所谓注册即告诉Spring,注册的属性由我来注入,不用你管了。可以看出我们注册了“age”和“birthday”属性。
那么这两个属性就由我们自己注入了。那么怎么注入呢,就是使用IntegerEditor()和DateEditor()方法。
下面是IntegerEditor()方法:
getAsText和setAsText是要从新定义的。其中getAsText方法在get方法请求时会调用,而setAsText方法在post方法请求时会调用。
至此复杂Command的绑定原理了解了。
如Integer,date,或自己定义的类时,controller就不会那么聪明了。
一般的做法是在自己的controller里override initBinder()方法。这里我们修改一下学习SimpleFormController的那个例子。
覆盖initBinder()方法
在执行请求参数到Command对象绑定之前,初始化一个可用的DataBinder实例,就是有了一个ServletRequestDataBinder之后,我们可以添加自定义的PropertyEdtior
以支持某些特殊数据类型的数据的绑定,或者排除某些不想绑定的请求参数,这些定制行为可以通过覆写initBinder()方法引入
下面的例子是在之前的SimpleFormController基础上进行了一些小小的变动
Command类中的主要变动时增加了一个Date类型的birthday字段,get字段的类型由int变成了Integer,这些都是不能直接将请求参数绑定到Command对象上
public class UserModel { private String account; private String phone; private String city; private Date createTime; private Date birthday; //将int改为Integer private Integer age;
在Controller里面覆盖initBinder()方法。
/** * 覆盖initBinder()方法 * 在执行请求参数到Command对象绑定之前,初始化一个可用的DataBinder实例,就是有了一个ServletRequestDataBinder之后,我们可以添加自定义的Property Edtior * 以支持某些特殊数据类型的数据的绑定,或者排除某些不想绑定的请求参数,这些定制行为可以通过覆写initBinder()方法引入 * void registerCustomEditor(Class<?> requiredType,String propertyPath,PropertyEditor propertyEditor) * requiredType - the type of the property. * propertyPath - the path of the property (name or nested path), or null if registering an editor for all properties of the given type * propertyEditor - editor to register */ protected void initBinder(HttpServletRequest req,ServletRequestDataBinder binder)throws Exception{ binder.registerCustomEditor(Integer.class, "age", new IntegerEditor()); binder.registerCustomEditor(Date.class,"birthday",new DateEditor()); }
可以看出使用了binder.registerCustomEditor方法,它是用来注册的。所谓注册即告诉Spring,注册的属性由我来注入,不用你管了。可以看出我们注册了“age”和“birthday”属性。
那么这两个属性就由我们自己注入了。那么怎么注入呢,就是使用IntegerEditor()和DateEditor()方法。
下面是IntegerEditor()方法:
public class IntegerEditor extends PropertyEditorSupport{ @Override public String getAsText() { Integer value=(Integer)getValue(); if(value==null){ value=new Integer(0); } return value.toString(); } @Override public void setAsText(String text) throws IllegalArgumentException { Integer value=null; if(text!=null&&!text.equals("")){ value=Integer.valueOf(text); } setValue(value); } }下面是DateEditor()方法:
public class DateEditor extends PropertyEditorSupport{ @Override public String getAsText() { Date value=(Date) getValue(); if(value==null){ value=new Date(); } SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); return df.format(value); } @Override public void setAsText(String text) throws IllegalArgumentException { Date value=null; if(text!=null&&!text.equals("")){ SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); try{ value=df.parse(text); }catch(Exception e){ e.printStackTrace(); } } setValue(value); } }
getAsText和setAsText是要从新定义的。其中getAsText方法在get方法请求时会调用,而setAsText方法在post方法请求时会调用。
至此复杂Command的绑定原理了解了。
相关文章推荐
- Springmvc学习笔记(4)之复杂参数类型绑定、校验、数据回显和异常处理
- springMVC学习笔记四(数据类型转换和数据验证)
- 黑马程序员-IOS学习笔记-C语言基础复杂数据类型
- springMVC+Mybatis 学习笔记-【3】Mybatis【批量别名定义】和【类型处理器】
- 【SpringMVC注解开发】案例驱动-包装类型pojo参数绑定 SpringMVC学习笔记(五)
- springmvc学习笔记(13)-springmvc注解开发之集合类型参数绑定
- springmvc学习笔记(12)-springmvc注解开发之包装类型参数绑定
- springMVC学习笔记四(数据类型转换和数据验证)
- springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定
- Apache CXF学习笔记二-复杂数据类型
- SpringMVC学习笔记(九)——简单类型、pojo类、自定义日期类型的参数绑定
- IOS开发学习笔记(十二)——ObjectC中的复杂类型
- Entity Framework with MySQL 学习笔记一(复杂类型 Complex Types)
- C++学习笔记(六):复杂数据类型(string、容器和STL)
- [原创]java WEB学习笔记68:Struts2 学习之路-- 类型转换与复杂属性配合使用
- 【C++】【学习笔记】【007-013】复杂的数据类型
- (原创)c#学习笔记05--变量的更多内容02--复杂的类型变量02--结构
- (原创)c#学习笔记05--变量的更多内容02--复杂的类型变量03--数组
- springmvc学习笔记(13)-springmvc注解开发之集合类型参数绑定
- Entity Framwork CodeFirst 学习笔记三:使用复杂类型