您的位置:首页 > 编程语言 > Java开发

Spring mvc 数据绑定 String 绑定 Date

2017-12-26 13:22 232 查看
package com.ccm.credit.manager.menu.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.springframework.format.Formatter;
import org.springframework.stereotype.Service;
@Service
public class DateFormater implements Formatter<Date>{

@Override
public String print(Date object, Locale locale) {
// TODO Auto-generated method stub
return null;
}

@Override
public Date parse(String text, Locale locale) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
return sdf.parse(text);
}

}


Spring boot 中 :formatter 接口 默认返回值是String,还可以实现Converter接口 用来自定义返回类型

Spring 中需要增加以下配置

//数据绑定(String绑定date)
<mvc:annotation-driven conversion-service="DateFormater"/>
<bean id="DateFormater" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="DateFormater">
<set>
<bean class="com.testone.Controller.DateFormater"></bean>
</set>
</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息