Spring mvc 數據綁定 String 綁定 Date


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>  




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章