spring propertyEditor 小总结

46 篇文章 0 订阅
33 篇文章 0 订阅

 

spring propertyEditor 小总结
2009-07-10 18:39

使用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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值