zoukankan      html  css  js  c++  java
  • FieldGroup绑定ItemDataSource

      FieldGroup可以直接绑定一个数据源DataSource。但如果想绑定某个值,并没有直接作为数据库中的一个字段存在。而是最后转为json串保存在数据库中。这样的话相当于key-value模式的DataSource,这里是ItemDataSource。

    为FieldGroup绑定一个日期控件

    //首先得在FieldGroup添加这个Item
    PropertysetItem item = new PropertysetItem();
    item.addItemProperty("{code_act_Date1}",new ObjectProperty(""));
    FieldGroup fieldGroup = new FieldGroup(item);
    
    //然后再进行定义日期控件
    WebGridLayout actGrid = componentsFactory.createComponent(WebGridLayout.class);
    DateField dateField = new DateField();
    dateField.setDateFormat("yyyy年MM月dd日");
    
    //进行绑定
    fieldGroup.bind(dateField, actElements.get(i).getValue());
    
    //将日期控件添加到页面中
    GridLayout tmpGgridLayout = (GridLayout) actGrid.getComponent();
    tmpGgridLayout.addComponent(dateField);

     PS:item.addItemProperty("{code_act_Date1}",new ObjectProperty(""));这句话为什么不是item.addItemProperty("{code_act_Date1}",new DateField());

    参见:http://www.cnblogs.com/acm-bingzi/p/cubaFieldGroupDate.html

    PPS:悲了个剧的,最后发现new ObjectProperty("")) 这种方法在某些服务器上会Date类型转换报错,所以这里还是应该使用 new DateField())

    一种类型转换错误的情况ClassCastException

    刚开始使用以下方式fieldGroup绑定

    WebGridLayout actGrid = componentsFactory.createComponent(WebGridLayout.class);
    WebDateField webDateField = componentsFactory.createComponent(WebDateField.class);
    webDateField.setDateFormat("yyyy年MM月dd日");
    fieldGroup.bind((DateField)webDateField.getComponent(), nameValueElements.get(i).getValue());
    actGrid.add(webDateField);

     报错:ClassCastException: com.haulmont.cuba.web.toolkit.ui.CubaDateFieldWrapper cannot be cast to com.vaadin.ui.DateField

    这个错真是很奇怪,但是上面那种把WebGridLayout 转换成GridLayout 就没有问题

     

    将FieldGroup中的数据转换成json串保存

    //...
    FieldGroup fieldGroup = new FieldGroup(item);
    Map<String, Object> mapValues = new HashMap<>();
    Collection<String> itemIds = (Collection<String>) item.getItemPropertyIds();
    for (String itemId : itemIds) {
        Object object = fieldGroup.getItemDataSource().getItemProperty(itemId).getValue();
        String value = null;
        if (object != null) {
            value = fieldGroup.getItemDataSource().getItemProperty(itemId).getValue().toString();
        }
        mapValues.put(itemId, value);
    }
    
    ObjectMapper mapper = new ObjectMapper();
    //转JSON
    String json = mapper.writeValueAsString(mapValues);

     最后json的值的格式是:

      {"{code_10_13}":"123","{code_10_15}":"接地电阻值满足要求","{code_7_6}":"智能建筑-防雷与接地"}

    再将json值保存到数据库中的一个字段

    将json串中的数据转换到FieldGroup中

    //简单写法:
    //这个方法里的两个参数,第一个是(PropertysetItem)fieldGroup.getItemDataSource(),第二个是json值
    void setSavedPropertyValues(PropertysetItem propertysetItem, String jsonValue) {
        Map<String, Object> mapValues = getSavedValue(jsonValue);
        if (mapValues != null) {
            for (String key : mapValues.keySet()) {
                if (propertysetItem.getItemProperty(key) != null) {
                    propertysetItem.getItemProperty(key).setValue(mapValues.get(key));
                }
            }
        }
    }
    
    //将json转换成ap类型
    Map<String, Object> getSavedValue(String jsonValue) {
        if (jsonValue == null || jsonValue.isEmpty()) {
            return null;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> mapValues = null;
        try {
            mapValues = objectMapper.readValue(jsonValue, Map.class);
        } catch (IOException e) {
            throw new IllegalArgumentException("已保持的值格式错误,无法解析成map对象");
        }
        return mapValues;
    }

      

    PS:如果最上边添加日期控件的时候,使用的是 item.addItemProperty("{code_act_Date1}",new DateField());

    那么这段代码应该稍微增加一点,如下:

    void setSavedPropertyValues(PropertysetItem propertysetItem, String jsonValue) {
        Map<String, Object> mapValues = getSavedValue(jsonValue);
        if (mapValues != null) {
            for (String key : mapValues.keySet()) {
                if (propertysetItem.getItemProperty(key) != null) {
                    if (propertysetItem.getItemProperty(key).getType() == Date.class) {
                        Date date = new Date((String) mapValues.get(key));
                        propertysetItem.getItemProperty(key).setValue(date);
                    } else {
                        propertysetItem.getItemProperty(key).setValue(mapValues.get(key));
                    }
                }
            }
        }
    }
  • 相关阅读:
    ApplicationContext之getBean方法详解
    Windows10终端优化方案:Ubuntu子系统+cmder+oh-my-zsh
    向 Windows 高级用户进阶,这 10 款效率工具帮你开路 | 新手问号
    Ditto —— windows 剪贴板增强小工具(复制粘贴多条记录)
    Service Mesh服务网格:是什么和为什么
    正确理解Spring事务和数据库事务和锁
    Spring中@Transactional事务回滚(含实例详细讲解,附源码)
    五分钟搞清楚MySQL事务隔离级别
    事务并发的问题场景图解
    Spring的事务管理和数据库事务相关知识
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/cubaFieldGroup.html
Copyright © 2011-2022 走看看