Java 确定在Vaadin中修改了哪些文本字段
我有一个数据源为Java 确定在Vaadin中修改了哪些文本字段,java,javabeans,textfield,vaadin,Java,Javabeans,Textfield,Vaadin,我有一个数据源为BeanItemContainer的表 当用户选择表中的一行时,相应的列将填充一组TextFields,以便用户可以编辑信息。我想做的是找到一种干净的方法来识别用户单击“保存”后编辑了哪些字段(这样我就可以跟踪用户所做的更改,并且只保存必要的内容) 我已经看到,我可以使用isModified()来查看字段是否与以前的值相比发生了变化,但是为每个TextField调用这个似乎太多了(当我在TextField上调用它时,isModified()似乎不起作用)。因此,我基本上是在寻找一
BeanItemContainer
的表
当用户选择表中的一行时,相应的列将填充一组TextFields
,以便用户可以编辑信息。我想做的是找到一种干净的方法来识别用户单击“保存”后编辑了哪些字段(这样我就可以跟踪用户所做的更改,并且只保存必要的内容)
我已经看到,我可以使用isModified()
来查看字段
是否与以前的值相比发生了变化,但是为每个TextField
调用这个似乎太多了(当我在TextField上调用它时,isModified()
似乎不起作用)。因此,我基本上是在寻找一种更好的方法来检查字段是否被修改
谢谢当您创建文本字段时,请将当前属性值(字符串)放入文本字段:
tf.setData(property.getValue())
当用户单击“保存”时,您可以比较两个值(当前值和保存值)。使用BeanItem属性。您必须自己比较值,但要在每个属性的循环中进行比较(请参阅源代码中的方法“between”)。您所需要的只是可以用作BeanItem或BeanItem本身的数据类。在UI中,您需要具有原始数据的对象和具有更改的原始数据的对象。下面是我用来提取两个数据版本之间的更改的类:
public class Diff implements Iterable<DiffEntry>{
public static class DiffEntry{
public final Object propertyId;
public final Object oldValue;
public final Object newValue;
public DiffEntry(Object propertyId, Object oldValue, Object newValue) {
super();
this.propertyId = propertyId;
this.oldValue = oldValue;
this.newValue = newValue;
}
}
public static <T> Diff between(T oldPojo, T newPojo) {
//HERE WE EXTRACT WHAT WAS CHANGED
// this could also take BeanItems directly if data are BeanItems
Diff diff = new Diff();
BeanItem<T> oldBean = new BeanItem<T>(oldPojo);
BeanItem<T> newBean = new BeanItem<T>(newPojo);
for(Object propertyId : oldBean.getItemPropertyIds()) {
Object oldValue = oldBean.getItemProperty(propertyId).getValue();
Object newValue = newBean.getItemProperty(propertyId).getValue();
if(oldValue == null) {
if(newValue != null) {
DiffEntry entry = new DiffEntry(propertyId, oldValue, newValue);
diff.add(entry);
}
}
else if(newValue == null) {
DiffEntry entry = new DiffEntry(propertyId, oldValue, newValue);
diff.add(entry);
}
else if(!oldValue.equals(newValue)) {
DiffEntry entry = new DiffEntry(propertyId, oldValue, newValue);
diff.add(entry);
}
else {
//old and new values are equal
}
}
return diff;
}
private final Map<Object, DiffEntry> entries = new HashMap<>();
public Diff() {
}
private void add(DiffEntry entry) {
this.entries.put(entry.propertyId, entry);
}
/**
* Returns true if this diff contains difference for specified property id
* @param propertyId id of property we test for difference
* @return true if this diff contains difference for specified property id
*/
public boolean contains(Object propertyId) {
return this.entries.containsKey(propertyId);
}
/**
* Returns true if there are no differencies
* @return true if there are no differencies
*/
public boolean isEmpty() {
return this.entries.isEmpty();
}
@Override
public Iterator<DiffEntry> iterator() {
return entries.values().iterator();
}
}
public类Diff实现了Iterable{
公共静态类差分{
公共最终对象属性ID;
公共最终目的价值;
公共最终目的价值;
public Differentry(对象属性ID、对象旧值、对象新值){
超级();
this.propertyId=propertyId;
this.oldValue=oldValue;
this.newValue=newValue;
}
}
公共静态差异(T oldPojo,T newPojo){
//在这里,我们提取出改变了的内容
//如果数据是BeanItems,则也可以直接获取BeanItems
Diff Diff=新的Diff();
BeanItem oldBean=新BeanItem(oldPojo);
BeanItem newBean=新的BeanItem(newPojo);
对于(对象propertyId:oldBean.GetItemPropertyId()){
对象oldValue=oldBean.getItemProperty(propertyId.getValue();
对象newValue=newBean.getItemProperty(propertyId.getValue();
if(oldValue==null){
if(newValue!=null){
Differentry条目=新的Differentry(propertyId、oldValue、newValue);
差异添加(条目);
}
}
else if(newValue==null){
Differentry条目=新的Differentry(propertyId、oldValue、newValue);
差异添加(条目);
}
如果(!oldValue.equals(newValue))为else{
Differentry条目=新的Differentry(propertyId、oldValue、newValue);
差异添加(条目);
}
否则{
//新旧价值观是平等的
}
}
返回差;
}
私有最终映射条目=新HashMap();
公共差异(){
}
专用作废添加(不同分录){
this.entries.put(entry.propertyId,entry);
}
/**
*如果此差异包含指定属性id的差异,则返回true
*@param propertyId我们测试的属性id是否存在差异
*@如果此差异包含指定属性id的差异,则返回true
*/
公共布尔包含(对象属性ID){
返回this.entries.containsKey(propertyId);
}
/**
*如果不存在差异,则返回true
*@如果没有差异,则返回true
*/
公共布尔值为空(){
返回此.entries.isEmpty();
}
@凌驾
公共迭代器迭代器(){
返回条目.values().iterator();
}
}
在您的“跟踪用户所做的更改,只保存必要的内容”上下文中,这将完成任务,但不会阻止从UI处理所有字段,因为这是在从字段读取所有数据并存储在newPojo中之后完成的 作为提示:当您使用像hibernate或eclipselink这样的ORM框架时,他们关心保存更改的值。我正在研究hibernate envers,但也想知道vaadin是否有自己的方法(或者GWT)。