您的位置:首页 > 其它

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

2015-07-16 10:09 489 查看
框架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("yyyy-MM-dd"));
*     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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: