其实PropertyEditor是JDK里面的java.beans下面的类,是提供AWT进行渲染用的。Spring通过利用该接口,来实现Bean的属性转换器。我们在Spring的xml或者其他途径配置的bean属性都是字符串类型的值,但是对应到每个具体的属性是各种类型的,Spring通过各种PropertyEditor来对各个属性进行类型转换,在Spring中的PropertyEditor并不是直接实现PropertyEditor接口,是通过PropertyEditorSupport类屏蔽了一些Spring不需要的方法比如paintValue,从而对它们提供了默认的实现,所以Spring里面的PropertyEditor都是在PropertyEditorSupport的基础上实现的。那么先看看Spring到底提供了哪些PropertyEditor的实现,下面截取了通过IDE工具找到的实现类截图
如果系统内置的editors 不能满足你的要求,比如想把 电话(0310-7425019) 自动映射到不同的模型属性中,就需要自定义 editor 完成数据转换操作。下面是自定义 editor的实现步骤:
1. 先编写一个模型类,用于封装参数信息
package org.demo1.model;
import java.util.Date;
public class DataBinderTestModel {
private String username;
private boolean bool;
private PhoneNumberModel phoneNumber;
private Date date;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
public PhoneNumberModel getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(PhoneNumberModel phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "DataBinderTestModel [username=" + username + ", bool=" + bool + ", phoneNumber=" + phoneNumber
+ ", date=" + date + "]";
}
}
package org.demo1.model;
public class PhoneNumberModel {
private String areaCode;
private String phoneNumber;
public String getAreaCode() {
return areaCode;
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "PhoneNumberModel [areaCode=" + areaCode + ", phoneNumber=" + phoneNumber + "]";
}
}
2. 继承自 PropertyEditorSupport ,编写一个电话号码转换Editor
package org.demo1.model;
import java.beans.PropertyEditorSupport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.util.StringUtils;
public class PhoneNumberModelEditor extends PropertyEditorSupport {
Pattern pattern = Pattern.compile("^(\\d{3,4})-(\\d{7,8})$");
@Override
public void setAsText(String text) throws IllegalArgumentException {
//如果没有填写,则设置为NULL
if(StringUtils.isEmpty(text)){
setValue(null);
return;
}
Matcher matcher = pattern.matcher(text);
if(matcher.matches()){
PhoneNumberModel phoneNumber = new PhoneNumberModel();
phoneNumber.setAreaCode(matcher.group(1));
phoneNumber.setPhoneNumber(matcher.group(2));
setValue(phoneNumber);
}else{
throw new IllegalArgumentException(String.format("类型转换失败,需要格式[010-12345678],但格式是[%s]", text));
}
}
@Override
public String getAsText() {
PhoneNumberModel model = (PhoneNumberModel)getValue();
return model.getAreaCode() + "-" + model.getPhoneNumber();
}
}
3. 注册 Editor
有两种方式可以完成注册:
1. 在controller中 添加如下代码
@InitBinder
public void initBinder(WebDataBinder binder) {
System.out.println("enter here");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
CustomDateEditor dateEditor = new CustomDateEditor(dateFormat, true);
binder.registerCustomEditor(Date.class, dateEditor);
binder.registerCustomEditor(PhoneNumberModel.class, new PhoneNumberModelEditor());
}
2. 不需要配置,把Editor放到和模型类同一包下,并且名称规则是 ModelClassName + Editor.java , spring MVC会自动查找
在地址栏输入如下地址测试:
http://localhost:8088/demo1/test/add?username=lisi&bool=1&phoneNumber=0310-7425019&date=2016-7-1
会显示如图所示的效果