我有一个数据表,它添加了编辑删除和过滤操作。
所有操作在数据库上运行正常,但在添加操作后数据表未正确更新。
第一次添加操作后,它会正确更新。 当我添加第二行时,数据表会更新,但首先添加的行将替换为第二行添加的行,但在数据库中它们会正确更新。
同样,当我添加第3行时,第1行将替换为第3行。
CRUD dataTable应用程序中使用的软件堆栈是: - PrimeFace 3.4.2,JSF 2.1,NetBeans 7.2.1,Java 1.6.0_37 GlassFish 3.1.x,MySql 5.x
这是我的代码。
**xhtml page**
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Prime Faces Data table</title>
</h:head>
<h:body>
<h:form id="form1">
<h:messages id="messages1"/>
<br/>
<p:commandButton id="Addbtn" value="Add" oncomplete="AddDialog.show()"
update=":addform"/>
<br/>
<p:dataTable id="dtusers" value="#{userdataController.retrieveData()}"
var="item"
rows="8" paginator="true" emptyMessage="No Records Found"
paginatorAlwaysVisible="false">
<p:column style="width:150px" filterMatchMode="contains"
filterBy ="#{item.id}">
<f:facet name="header"> Id </f:facet>
<h:outputText value="#{item.id}"/>
</p:column>
<p:column style="width:150px" filterMatchMode="contains"
filterBy="#{item.name}">
<f:facet name="header"> Name </f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column style="width:150px">
<p:commandButton id="editButton" update=":editform"
oncomplete="EditDialog.show()" value="Edit">
<f:setPropertyActionListener value="#{item}"
target="#{userdataController.selectedUser}"/>
</p:commandButton>
</p:column>
<p:column style="width:150px">
<p:commandButton id="deleteButton" update=":deleteform"
oncomplete="UserDialog.show()" value="Delete">
<f:setPropertyActionListener value="#{item}"
target="#{userdataController.selectedUser}"/>
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<p:dialog header="Add User Data" resizable="false" id="AddDialog"
widgetVar="AddDialog" modal="true" showEffect="fade" hideEffect="fade"
position="center" >
<h:form id="addform">
<p:outputPanel id="AddDisplay">
<p:panelGrid columns="2" >
<f:facet name="header">
Add Userdata
</f:facet>
<h:outputLabel for="Id" value="Enter Id:"/>
<p:inputText id="Id" value="#{userdataController.selected.id}" />
<h:outputLabel for="Name" value="Enter Name:"/>
<p:inputText id="Name"
value="#{userdataController.selected.name}"/>
<p:commandButton actionListener="#{userdataController.createdata}"
update=":form1:dtusers :form1:messages1" value="Save"
oncomplete="AddDialog.hide()" style="margin:0"/>
</p:panelGrid>
</p:outputPanel>
</h:form>
</p:dialog>
//similarly dialogs for delete and edit
</h:body>
</html>
**ManagedBean**
@ManagedBean(name = "userdataController")
@SessionScoped
public class UserdataController implements Serializable {
private Userdata current;
private DataModel items = null;
@EJB
private PrimeFacesDb.session.UserdataFacade ejbFacade;
private int id;
private String name;
private Userdata SelectedUser;
public UserdataController() {
}
//getters setters for id,name and SelectedUser
private UserdataFacade getFacade() {
return ejbFacade;
}
public List retrieveData() {
List result = getFacade().findAll();
return result;
}
public void create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage("Userdata created successfully");
} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Persistence Error occured");
}
}
}
答案 0 :(得分:0)
使用process =“:form2:Add Display”,我认为这是你的问题,因为这个过程默认了它的整个视图,或者你的findAll()中有一个缓存,或者现在是jeje