简体   繁体   English

p:dataTable rowEdit中的已编辑/更新值在侦听器方法中不可用,因为它们被数据库中的现有数据覆盖

[英]Edited/updated values in p:dataTable rowEdit are not available in listener method as they are being overwritten by existing data from database

I'm editing data with <p:dataTable> row editor as below. 我正在使用<p:dataTable>行编辑器编辑数据,如下所示。

<p:dataTable value="#{bean.users}" var="user" editable="true">
    <p:ajax event="rowEdit" listener="#{bean.onRowEdit}" />
    <p:ajax event="rowEditCancel" listener="#{bean.onRowEditCancel}" />
    <p:column>
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{user.firstName}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{user.firstName}" />
            </f:facet>
        </p:cellEditor>
    </p:column>
</p:dataTable>

The backing bean is implemented as below. 支持bean的实现如下。

private List<User> users;

@EJB
private UserService userService;

public List<User> getUsers() {
    users = userService.list();
    return users;
}

When I enter the new data in the cellEditor and submit it, they are not available in listener method. 当我在cellEditor中输入新数据并提交它时,它们在侦听器方法中不可用。 I noticed that they get overwritten by the data called from the database. 我注意到它们被数据库调用的数据覆盖了。

Why does this happen and how can I avoid it? 为什么会发生这种情况,我该如何避免呢?

Your problem is caused by performing business logic in a getter method. 您的问题是由getter方法中的业务逻辑引起的。 Every iteration over the data table will invoke the getter method. 数据表上的每次迭代都将调用getter方法。 So, while JSF is busy iterating over the data table in order to set the submitted values in the model, the getter calls returns a new list from DB again and again. 因此,当JSF忙于迭代数据表以便在模型中设置提交的值时,getter调用会一次又一次地从DB返回一个新列表。

You're not supposed to perform business logic in a getter method. 您不应该在getter方法中执行业务逻辑。 As long as you're a starter, you'd better refrain from touching the getter (and setter) methods and perform the job elsewhere in an one time called method. 只要你是初学者,你最好不要触及getter(和setter)方法,并在一次性调用方法中执行其他工作。

You likely need a @PostConstruct (and a true service/DAO class) here: 您可能需要@PostConstruct (以及真正的服务/ DAO类):

private List<User> users;

@EJB
private UserService userService;

@PostConstruct 
public void init() {
    users = userService.list(); // Call the DB here.
}

public List<User> getUsers() {
    return users; // Just return the already-prepared model. Do NOT do anything else here!
}

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 p:dataTable的rowEdit事件上的Bean方法未调用 - Bean method on rowEdit event of p:dataTable is not invoked p:dataTable-通过menuButton切换rowEdit - p:dataTable - toggle rowEdit via menuButton p:dataTable在对话框关闭时未显示数据库中的更新记录 - p:dataTable not showing updated records from database on Dialog close p:treeTable-未调用p:ajax侦听器方法 - p:treeTable - p:ajax listener method not being called bean中的方法,用于检查Primefaces数据表中的行是否仍在编辑中 - Method in bean that checks whether a row in a Primefaces datatable is still being edited 如何从p:dataTable的p:列中的p:cellEditor获取已编辑单元格的旧/新值 - How to get old/new value of edited cell from p:cellEditor in p:columns of p:dataTable primefaces 数据表:rowedit 不起作用 - primefaces datatable :rowedit not working 带有 contextMenu、multipleSelection、rowEdit 的数据表 - DataTable with contextMenu, multipleSelection, rowEdit 如何使用 JSF 将 p:dataTable 中的多个 p:rating 值保存到数据库中? - How to save multiple p:rating values from p:dataTable into database using JSF? 如何通知对p:dataTable的行的更改,该行当前正在编辑到相应的backing bean? - How to notify changes to a row of p:dataTable which is currently being edited to a corresponding backing bean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM