ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

spring mvc使用@InitBinder标签对表单数据绑定(1/2)

来源:网络整理     时间:2016-03-12     关键词:

本篇文章主要介绍了"spring mvc使用@InitBinder标签对表单数据绑定",主要涉及到方面的内容,对于其他编程jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: 在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。 解...

        在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。

        解决的办法就是使用spring mvc提供的@InitBinder标签。

        在我的项目中是在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。剩下的控制器都继承该类。spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。

        当然,我们也可以不使用他自己自带这些编辑器类,下面我们自己去构造几个。

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class DoubleEditor extends PropertiesEditor {  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (text == null || text.equals("")) {  
            text = "0";  
        }  
        setValue(Double.parseDouble(text));  
    }  

    @Override  
    public String getAsText() {  
        return getValue().toString();  
    }  
}
import org.springframework.beans.propertyeditors.PropertiesEditor;

public class IntegerEditor extends PropertiesEditor {  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (text == null || text.equals("")) {  
            text = "0";  
        }  
        setValue(Integer.parseInt(text));  
    }  

    @Override  
    public String getAsText() {  
        return getValue().toString();  
    }  
}
import org.springframework.beans.propertyeditors.PropertiesEditor;

public class FloatEditor extends PropertiesEditor {  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (text == null || text.equals("")) {  
            text = "0";  
        }  
        setValue(Float.parseFloat(text));  
    }  

    @Override  
    public String getAsText() {  
        return getValue().toString();  
    }  
}
import org.springframework.beans.propertyeditors.PropertiesEditor;

public class LongEditor extends PropertiesEditor {  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (text == null || text.equals("")) {  
            text = "0";  
        }  
        setValue(Long.parseLong(text));  
    }  

    @Override  
    public String getAsText() {  
        return getValue().toString();  
    }  
}
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.StringUtils;

public class CustomDateEditorWapper extends PropertyEditorSupport {
    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final boolean allowEmpty;

    /**
     * 是否允许为空
     * @param allowEmpty
     */
    public CustomDateEditorWapper(boolean allowEmpty) {
        this.allowEmpty = allowEmpty;
    }

    /**
     * 根据格式绑定数据
     */
    public void setAsText(String text) throws IllegalArgumentException {
        if (this.allowEmpty && !StringUtils.hasText(text)) {
            setValue(null);
        } else if (text.indexOf("1") == 0 && text.length() == 13 && text.indexOf("-") < 0) {
            //日期传Long的转换
            setValue(new Date(Long.parseLong(text)));
        } else {
            setValue(DateUtils.parseTime(text));
        }
    }

    public String getAsText() {
        Date value = (Date) getValue();
        return (value != null ? this.dateFormat.format(value) : "");
    }
}

        在BaseController中。

相关图片

相关文章