zoukankan      html  css  js  c++  java
  • 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>
     * &#064;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);
                }
            }
        }
    }
  • 相关阅读:
    深入理解计算机系统读书笔记之第二章信息的表示和处理
    深入理解计算机系统读书笔记之第一章:漫游
    算法三之归并排序
    算法二之子集和数问题
    算法一之N皇后问题
    刚刚注册写一写
    linux alias(命令别名)
    linux [CTRL]+c与[CTRL]+d
    linux终端类型
    linux 进程简介
  • 原文地址:https://www.cnblogs.com/zrui-xyu/p/4650394.html
Copyright © 2011-2022 走看看