mybatis前台传给带年月日时分秒的数据给后台,后台接收不到时分秒

时间:2023-03-09 00:23:18
mybatis前台传给带年月日时分秒的数据给后台,后台接收不到时分秒

框架spring+springMVC+mybatis,

前台给后台传数据传不了时分秒,所以用springMVC的注解解决了,记录一下

controller中如下:

/**
*
* 方法描述 : 使用@InitBinder 标签对表单数据绑定
* @param binder
*/
@InitBinder//spring mvc使用@InitBinder 标签对表单数据绑定
public void initBinder(WebDataBinder binder) {//WebDataBinder是用来绑定请求参数到指定的属性编辑器
binder.registerCustomEditor(Date.class, new DateConvertEditor("yyyy-MM-dd HH:mm:ss")); }
DateConvertEditor
package cn.edu.hbcf.vo;

import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat; import org.apache.commons.lang.StringUtils; /**
* spring中日期转换
*
* <pre>
* @InitBinder
* public void initBinder(WebDataBinder binder) {
* binder.registerCustomEditor(Date.class, new DateConvertEditor());
* // binder.registerCustomEditor(Date.class, new
* // DateConvertEditor(&quot;yyyy-MM-dd&quot;));
* binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
* }
* </pre>
*
*
* @author LiPenghui
* @date 2011-8-10 下午1:48:37
*/
public class DateConvertEditor extends PropertyEditorSupport{
private DateFormat format; public DateConvertEditor(){
this.format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} public DateConvertEditor(String format){
this.format=new SimpleDateFormat(format);
} /** Date-->String **/
public String getAsText(){
if(getValue()==null){
return "";
}
return this.format.format(getValue());
} /** String-->Date **/
public void setAsText(String text){
if(!StringUtils.isNotBlank(text)){
setValue(null);
}else{
try {
setValue(this.format.parse(text));
} catch (ParseException e) {
throw new IllegalArgumentException("不能被转换的日期字符串,请检查!", e);
}
}
}
}