2016-08-23 44 views
1

我在理解Vaadin中的commit()方法真的有些问题。我阅读了文档,做了一些例子,但我仍然误解它的实际内容。Vaadin Form的commit()方法?

这是代码片段:

if (source == save) { 
    /* If the given input is not valid there is no point in continuing */ 
    if (!isValid()) { 
     return; 
    } 

    if (newContactMode) { 
     /* We need to add the new person to the container */ 
     Item addedItem = app.getDataSource().addItem(newPerson); 
     /* 
      * We must update the form to use the Item from our datasource 
      * as we are now in edit mode 
      */ 
     setItemDataSource(addedItem); 
     //System.out.println(app.getDataSource().getItem(addedItem)); 
     newContactMode = false; 
    } 

    commit(); 
    setReadOnly(true); 
} 

如果我这样做事,那么我不添加一些我在表单中的数据源写的数据(在容器)的。这些条目未在表格中显示。

另一个代码片段:

if (source == save) { 
    /* If the given input is not valid there is no point in continuing */ 
    if (!isValid()) { 
     return; 
    } 
    commit();//changing place of this method 
    if (newContactMode) { 
     /* We need to add the new person to the container */ 
     Item addedItem = app.getDataSource().addItem(newPerson); 
     /* 
      * We must update the form to use the Item from our datasource 
      * as we are now in edit mode 
      */ 
     setItemDataSource(addedItem); 
     //System.out.println(app.getDataSource().getItem(addedItem)); 
     newContactMode = false; 
    } 
    setReadOnly(true); 
} 

这个版本可以正常工作。我可以得出结论,表单中的这个方法会阻止与此表单的“DataSource”(包含DataSource中的项目)的所有交互。但我需要通过直接调用另一个类和Container addItem()来手动完成。我还没有找到任何关于commit()方法的好解释。

我一直在使用这tutorial,也许有人会从教程中认识到这个GUI。

GUI

+0

在哪个类中实现了commit()方法?应该有一个文件。 Vaadin在generel中有很好的记录。 – d2k2

+0

commit()比文档说的做更多的事情。请阅读我的问题 – Alex

+0

你还在vaadin 6? –

回答

0

我明白了什么是错的。在第二个片段中,我通过调用commit()从form的数据填充我的newPerson实例。在第一个片段中,我写了空数据,因为我之前没有调用方法commit(),并且绑定对象为null(尚未写入)。

相关问题