从数据库中删除的primefaces行

时间:2014-06-02 07:01:31

标签: java primefaces datatable row

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

的index.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;
    }
}

3 个答案:

答案 0 :(得分:2)

因为你只是从arraylist中删除它而不是从数据库中删除它
使用em.remove(user)

答案 1 :(得分:0)

您的删除操作仅将其从范围变量中删除。

public String deleteAction(User user) {
    userList.remove(user);
    return null;
}

您还需要通过实体管理器将其从数据库中删除。 em.remove(对象)

答案 2 :(得分:0)

使用remove方法尝试使用EntityManager执行以下代码。

@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) {
        EntityManager em = emf.createEntityManager();
        em.remove(user);
        userList.remove(user);
        return null;
    }
}