使用spring的mvc感觉比较顺利,这段时间因为钱和女人的事情而分心,今天用mulitAcitonController binder一个entity的时候,发现实体中的 一个属性 private Timestamp birthday的时候绑定不起来。于是网上找了这样一个propertyEditor,明白了 propertyEditor的作用。
package cn.hqsoft.safeFile.propertyeditor;
import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.util.StringUtils;
public class DatePropertyEditor extends PropertyEditorSupport {
private String format = "yyyy年MM月dd日"; //缺省
public void setFormat(String format) {
this.format = format;
}
public void setAsText(String text) throws IllegalArgumentException {
if (text != null || StringUtils.hasText(text)) {
try {
setValue(new Timestamp((new SimpleDateFormat(this.format).parse(text)).getTime()));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
接着在我的controller中
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
super.initBinder(request, binder);
binder.registerCustomEditor(Timestamp.class, new DatePropertyEditor()
}
spring的Editor用处比较大。自动绑定做的很人性化,省略了很多无聊代码,
这段时间用ext觉得有苦有甜,苦的是表单布局。甜的是其他的。复杂的表单布局,很让人头晕。
http://hi.baidu.com/32647908/blog/item/08601ef47ee129d0f2d385c6.html
http://code.google.com/p/jdbexplorer/source/browse/trunk/src/main/java/cn/com/qimingx/spring/BaseMultiActionController.java?spec=svn30&r=30
http://code.google.com/p/jdbexplorer/source/browse/trunk/src/main/java/cn/com/qimingx/spring/CustomJsonDateEditor.java?r=30