zoukankan      html  css  js  c++  java
  • Spring DataBinder

    两种构造方式创建数据绑定组件:

    /**
     * Create a new DataBinder instance, with default object name.
     * @param target the target object to bind onto (or {@code null}
     * if the binder is just used to convert a plain parameter value)
     * @see #DEFAULT_OBJECT_NAME
     */
    public DataBinder(@Nullable Object target) {
    	this(target, DEFAULT_OBJECT_NAME);
    }
    
    /**
     * Create a new DataBinder instance.
     * @param target the target object to bind onto (or {@code null}
     * if the binder is just used to convert a plain parameter value)
     * @param objectName the name of the target object
     */
    public DataBinder(@Nullable Object target, String objectName) {
    	this.target = ObjectUtils.unwrapOptional(target);
    	this.objectName = objectName;
    }
    

      

    说明:

    or null if the binder is just used to convert a plain parameter value【 如果仅使用数据绑定组件的类型转换功能,则可以将target对象设置为null。】

    类型转换功能(实现了TypeConvert[org.springframework.beans.TypeConverter]接口,因此拥有类型转换功能):  
    @Nullable
    <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType) throws TypeMismatchException;
    
    @Nullable
    <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType,
    		@Nullable MethodParameter methodParam) throws TypeMismatchException;
    
    @Nullable
    <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType, @Nullable Field field)
    		throws TypeMismatchException;
    
    @Nullable
    default <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType,
    		@Nullable TypeDescriptor typeDescriptor) throws TypeMismatchException {
    
    	throw new UnsupportedOperationException("TypeDescriptor resolution not supported");
    }
    

      

    使用方式:

    DataBinder db = new DataBinder(null);
    
    Integer s = db.convertIfNecessary("324", Integer.class);
    Double aDouble = db.convertIfNecessary("234.32", Double.class);
    Integer integer = db.convertIfNecessary(1232.633, Integer.class);
    Integer[] integers = db.convertIfNecessary(new String[]{"123", "456"}, Integer[].class);
    

      






  • 相关阅读:
    tornado硬件管理系统-数据存储与表格实时监控(8)
    db2 内存研究
    Oracle dml开始到commit期间的流程
    用户界面与业务逻辑的分离
    计算器核心算法——终结版
    计算器核心算法——中缀表达式转为后缀表达式
    计算器核心解析算法(上)
    Qt中的字符串类
    初探Qt中的消息处理
    计算器界面代码重构
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/12166642.html
Copyright © 2011-2022 走看看