Java 就地保存 PrimeFaces 中更改的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9790146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving changed values from PrimeFaces inplace
提问by Klaasvaak
I Want to save the value of an inplace if it was changed.
如果已更改,我想保存就地的值。
The methode employeeController.save() is called when I click on the save button. But how can I pass along the new and old value? I want to do this so I can know if the value was changed without asking the database.
当我单击保存按钮时调用方法 employeeController.save()。但是我怎样才能传递新旧值呢?我想这样做,这样我就可以在不询问数据库的情况下知道值是否已更改。
<h:panelGrid id="display" columns="2" cellpadding="4"
style="width:300px;"
styleClass="ui-widget-content"
columnClasses="label, value">
<h:outputText value="ID:" />
<h:outputText id="ID" value="#{emp.id}" />
<h:outputText value="Voornaam:"/>
<p:inplace id="firstnam" editor="true">
<p:ajax event="save" onsuccess="#{employeeController.saveName()}">
</p:ajax>
<p:inputText id ="firstName" value="#{emp.firstName}"
required="true" label="text"/>
</p:inplace>
采纳答案by BalusC
There the valueChangeListener
is for.
那里valueChangeListener
是为了。
E.g.
例如
<p:inputText ... valueChangeListener="#{employeeController.firstNameChanged}" />
with
和
public void firstNameChanged(ValueChangeEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
// ...
}
You could set a boolean
there or collect the values in some other property and handle it further in the real command action method.
您可以在boolean
那里设置一个或收集其他一些属性中的值,并在真正的命令操作方法中进一步处理它。
The method will only be invoked when the value is really changed. Even if it's just from null
to empty string. It will not be invoked when the old value equals()
the new value.
只有当值真正改变时才会调用该方法。即使它只是从null
到空字符串。当旧值变为equals()
新值时不会调用它。
回答by Rafael Ruiz Tabares
In mi case I can not use event="save", so I use p:event event="valueChange". In the managedBean I have properties that it will be modified and the object owner this properties. I see many people use event="save"but in my case this event throw render view error, for this reason I use valueChange
在 mi 的情况下,我不能使用event="save",所以我使用p:event event="valueChange"。在 managedBean 我有将被修改的属性和对象所有者这个属性。我看到很多人使用event="save"但在我的情况下这个事件会引发渲染视图错误,因此我使用valueChange
code view
代码视图
<p:column>
<p:inplace id="inplaceNombre" emptyLabel="#{registro.usuario.nombre}" editor="true">
<p:inputText id="nombre" required="true"
requiredMessage="#{msg['editar.nombre.required']}"
alt="#{msg['editar.alt.nombre']}" title="#{msg['editar.title.nombre']}"
tabindex="1" value="#{registro.nombre}"
styleClass="#{component.valid ? '' : 'invalid'}" maxlength="30">
<f:validateBean for="nombre" />
<p:ajax event="valueChange" update="@this messageNombre" />
</p:inputText>
</p:inplace>
</p:column>
<p:row>
<p:column>
<p:commandButton value="#{msg['editar.value.enviar']}"
title="#{msg['editar.title.enviar']}" alt="#{msg['editar.alt.enviar']}"
actionListener="#{registro.actualizarUsuario}" tabindex="7" />
</p:column>
</p:row>
managedBean code
托管Bean代码
//property that can be modified in the User
@Size(min=5 ,max=30 ,message="Este campo no es correcto: tama?o máximo [30]")
@Pattern(regexp="^[a-zA-Z]+[a-zA-Z ]+",message="Este campo no es correcto")
private String nombre;
//User get by database with f:event type=preRenderView in xhtml view
private Usuario usuario;
Then, I check the changes and update user.
然后,我检查更改并更新用户。
Kind Regards.
亲切的问候。
回答by Danubian Sailor
General solution: store the original data object, provide the copy from modifications.
一般解决方案:存储原始数据对象,提供修改后的副本。
You will there be always able to say what fields have changed since last save. In the case of value change listeners, you'll know only if the value has changed since last submit, which not always is the same.
您将始终能够说出自上次保存以来哪些字段发生了变化。对于值更改侦听器,您只会知道自上次提交以来值是否发生更改,这并不总是相同。
Remembering the old values is a good practise, because you can easily revert the changes made by user in UI, check if update to database is really necessary etc.
记住旧值是一个很好的做法,因为您可以轻松地恢复用户在 UI 中所做的更改,检查是否真的需要更新数据库等。