gpt4 book ai didi

java - 从数据库中删除 primefaces 行

转载 作者:行者123 更新时间:2023-12-02 05:46:43 26 4
gpt4 key购买 nike

我正在尝试从数据库中删除 primefaces 数据表中的行。它在数据表中工作正常,但刷新项目后,行值显示出来。我正在使用 http://www.mkyong.com/jsf2/how-to-delete-row-in-jsf-datatable/例子。有人可以帮忙吗?

索引.xhtml

    <p:growl id="messages" showDetail="true"/>  

<p:dataTable var="u" value="#{logonTest.userList}" id="carList" editable="true">

<f:facet name="header">
In-Cell Editing
</f:facet>

<p:ajax event="rowEdit" listener="#{tableBean.onEdit}" update=":form:messages" />
<p:ajax event="rowEditCancel" listener="#{tableBean.onCancel}" update=":form:messages" />

<p:column headerText="Name" style="width:30%">

<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.name}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{u.name}" style="width:100%"/>
</f:facet>

</p:cellEditor>

</p:column>

<p:column headerText="Surname" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.surname}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{u.surname}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>

<p:column headerText="Username" style="width:24%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.username}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{u.username}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>

<p:column headerText="Description" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.description}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{u.description}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>

<p:column style="width:6%">
<p:rowEditor />
<h:commandLink value="Delete" action="#{logonTest.deleteAction(u)}" />
</p:column>

</p:dataTable>

</h:form>

LogonTest.java

@ViewScoped
@SessionScoped
@javax.faces.bean.ManagedBean(name = "logonTest")
public class LogonTest implements Serializable{
@PersistenceUnit(unitName="Webbeans_RESOURCE_LOCAL")
private EntityManagerFactory emf;
public List<User> getUserList() {
return userList;
}

public void setUserList(List<User> userList) {
this.userList = userList;
}

public List<User> userList = new ArrayList();


@PostConstruct
public void init(){
EntityManager em = emf.createEntityManager();
// Read the existing entries and write to console
Query q = em.createQuery("SELECT u FROM User u");
userList = q.getResultList();
System.out.println("Size: " + userList.size());
}

public LogonTest() {

}
public String deleteAction(User user) {

userList.remove(user);
return null;
}
}

最佳答案

因为您仅将其从数组列表中删除,而不是从数据库中删除
使用em.remove(user)

关于java - 从数据库中删除 primefaces 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23988906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com