springMVC属性值绑定(日期类型转成Timestamp)_浮梦丶天拓_新浪博客

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import org.springframework.util.StringUtils;
import java.text.ParseException;

public class CustomTimestampEditor extends PropertyEditorSupport {
private final SimpleDateFormat dateFormat;
    private final boolean allowEmpty;
    private final int exactDateLength;
 
    public CustomTimestampEditor(SimpleDateFormat dateFormat, boolean allowEmpty) {
        this.dateFormat = dateFormat;
        this.allowEmpty = allowEmpty;
        this.exactDateLength = -1;
    }
 
    public CustomTimestampEditor(SimpleDateFormat dateFormat,
            boolean allowEmpty, int exactDateLength) {
        this.dateFormat = dateFormat;
        this.allowEmpty = allowEmpty;
        this.exactDateLength = exactDateLength;
    }
 
    public void setAsText(String text) throws IllegalArgumentException {
        if ((this.allowEmpty) && (!(StringUtils.hasText(text)))) {
            setValue(null);
        } else {
            if ((text != null) && (this.exactDateLength >= 0)
                    && (text.length() != this.exactDateLength)) {
                throw new IllegalArgumentException(
                        "Could not parse date: it is not exactly"
                                + this.exactDateLength + "characters long");
            }
            try {
                setValue(new Timestamp(this.dateFormat.parse(text).getTime()));
            } catch (ParseException ex) {
                throw new IllegalArgumentException("Could not parse date: "
                        + ex.getMessage(), ex);
            }
        }
    }
 
    public String getAsText() {
        Timestamp value = (Timestamp) getValue();
        return ((value != null) ? this.dateFormat.format(value) : "");
    }

}

上面为自定义的类就是将前台日期格式转换成timestamp类型。
然后再controller类的最前面加入如下方法:
@InitBinder    
public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");    
        dateFormat.setLenient(false);    
        binder.registerCustomEditor(Timestamp.class, new CustomTimestampEditor(dateFormat, true));    
}

解释:
binder.registerCustomEditor(Timestamp.class, new CustomTimestampEditor(dateFormat, true));
这个方法中的new的类可以是自定义的类,也可以使用一些已有的类即已经继承并实现PropertyEditorSupport类的类。
具体默认的类有哪些可以去看PropertyEditorSupport这个类中的源码,因为你自定义的类也是继承的这个类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC提供了三种类转换的接口,分别是Formatter、Converter和原生类转换器。 1. Formatter接口:Formatter接口用于将字符串转换为目标类,并将目标类转换为字符串。它可以用于处理日期、数字等常见的数据类转换。Formatter接口定义了两个方法:`parse()`用于将字符串转换为目标类,`print()`用于将目标类转换为字符串。 2. Converter接口:Converter接口用于将一种数据类转换为另一种数据类。它定义了两个方法:`convert()`用于将源类转换为目标类,`reverse()`用于将目标类转换回源类。开发者可以根据自己的需求实现Converter接口来实现特定功能的类转换。 3. 原生类转换器:SpringMVC内置了许多原生类转换器,可以将常见的数据类进行转换,例如将字符串转换为整、将字符串转换为日期等。开发者在实际应用中使用框架内置的类转换器基本上就够了,但有时需要编写具有特定功能的类转换器。 下面是一个使用Formatter接口的示例: ```java import org.springframework.format.Formatter; public class MyDateFormatter implements Formatter<Date> { @Override public Date parse(String text, Locale locale) throws ParseException { // 将字符串转换为日期类 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.parse(text); } @Override public String print(Date date, Locale locale) { // 将日期类转换为字符串 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.format(date); } } ``` 下面是一个使用Converter接口的示例: ```java import org.springframework.core.convert.converter.Converter; public class MyStringToIntegerConverter implements Converter<String, Integer> { @Override public Integer convert(String source) { // 将字符串转换为整 return Integer.parseInt(source); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值