2

我有一个具有添加编辑删除和过滤操作的数据表。

所有操作在数据库上都可以正常工作,但是在添加操作后数据表没有正确更新。

它在第一次添加操作后正确更新。当我添加第二行时,数据表已更新,但首先添加的行被第二行添加的行替换,但在数据库中它们已正确更新。

同样,当我添加第三行时,第一行被第三行替换。

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");
    }
  }
 }
4

0 回答 0