CellEditEvent event.getNewValue()保持返回旧值

时间:2014-09-03 18:43:45

标签: jsf primefaces

我目前有一个表面DataTable,editMode设置为单元格。当我为给定的单元格输入一个新值时,它不想获得我刚刚输入的最新值,而是继续返回现有的(旧值)

例如,如果旧值为7,并且我将其编辑为3,则event.getNewValue()将返回7而不是3。

这是我的xhtml文件:

    <?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<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:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Admin</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </h:head>
    <body>

        <ui:composition template="template.xhtml">

            <ui:define name="content">
                <div id="header">
                    <div id="logo">
                        <h:form>
                            <h:link outcome="adminHome">
                                <h:graphicImage value="resources/img/logo.png" alt="Monash South Africa - A campus of Monash Univeristy Australia"/>
                            </h:link>
                        </h:form>
                    </div>

                    <ul>
                        <h:form>
                            <li>Help</li>
                            <li><h:link value="Logout" outcome="login"></h:link></li>
                        </h:form>
                    </ul>
                </div>

                <h:form id="adminHome">
                    <p:menubar>
                        <p:menuitem value="Home" icon="ui-icon-home" url="faces/userHome.xhtml" style="margin-left: 0px !important; margin-right: 0px !important;"/>
                        <p:menuitem value="View Applications" icon="ui-icon-document" url="faces/userHome.xhtml" style="margin-left: 0px !important; margin-right: 0px !important;"/>
                        <p:submenu label="Manage" icon="ui-icon-document" style="margin-left: 0px !important; margin-right: 0px !important;">                        
                            <p:menuitem value="GPA Grants" url="#" style="padding-left: 0% !important; margin-left: 0% !important;"/>
                            <p:menuitem value="Achievement Levels" url="#" style="padding-left: 0% !important; margin-left: 0% !important;"/>
                            <p:menuitem value="Marks" url="#" style="padding-left: 0% !important; margin-left: 0% !important;"/>
                        </p:submenu>
                    </p:menubar>
                    <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
                    <h3>List of Applications</h3>                    

                    <p:dataTable id="appDT" value="#{applicationFacade.applications}" var="item" resizableColumns="true" liveResize="true" selection="#{applicationFacade.appList}" rowKey="#{item.applicationid}" paginator="true" rows="50" rowsPerPageTemplate="50,100,150" editable="true" editMode="cell">

                        <p:ajax event="cellEdit" listener="#{applicationFacade.onCellEdit}" update="appDT"/>

                        <p:column selectionMode="multiple" style="width:16px;text-align:center"/>                        
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Studentid"/>
                            </f:facet>
                            <h:outputText value="#{item.studentid}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Email"/>
                            </f:facet>
                            <h:outputText value="#{item.email}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Coursecode"/>
                            </f:facet>
                            <h:outputText value="#{item.coursecode}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Levelofstudy"/>
                            </f:facet>
                            <h:outputText value="#{item.levelofstudy}"/>
                        </p:column>                            
                        <p:column headerText="AL">
                            <p:cellEditor>
                                <f:facet name="output"><h:outputText value="#{item.al}"/></f:facet>
                                <f:facet name="input"><p:inputText value="#{item.al}"/></f:facet>
                            </p:cellEditor>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="GPA"/>
                            </f:facet>
                            <h:outputText value="#{item.gpa}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="AVG"/>
                            </f:facet>
                            <h:outputText value="#{item.average}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Approvalcount"/>
                            </f:facet>
                            <h:outputText value="#{item.approvalcount}"/>
                        </p:column>

                        <f:facet name="footer">
                            <p:commandButton action="#{applicationFacade.approveApplication()}" icon="ui-icon-check" value="Approve" update="appDT"/>
                            <p:commandButton action="#{applicationFacade.rejectApplication()}" icon="ui-icon-closethick" value="Reject" update="appDT"/>
                        </f:facet>                        



                    </p:dataTable>
                </h:form>

            </ui:define>

        </ui:composition>


    </body>
</html>

这是我的豆子

package DataAccess;

import Entities.Application;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.primefaces.event.CellEditEvent;


@Stateless
@Named(value = "applicationFacade")
public class ApplicationFacade extends AbstractFacade<Application> {

    @PersistenceContext(unitName = "com.mycompany_WSAMS_war_1.0-SNAPSHOTPU")
    private EntityManager em;
    private List<Application> appList;
    private List<Application> removeList;
    private Application appRef;
    private Application appRef1;    
    private int approvalCounter;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ApplicationFacade() {
        super(Application.class);
    }

    public List<Application> getAppList() {
        return appList;
    }

    public void setAppList(List<Application> appList) {
        this.appList = appList;
    }

    //   ----------------------------------------------------------------------------Business------------------------------------------------------------------
    public List<Application> getApplications() {
        return em.createNamedQuery("Application.findAll").getResultList();
    }

    public void approveApplication() {
        for (Application each : appList) {
            approvalCounter = each.getApprovalcount();
            approvalCounter++;

            if (approvalCounter > 6) {
                infoSix();
            } else {
                each.setApprovalcount(approvalCounter);
                appRef = em.merge(each);
                em.persist(appRef);
            }
        }
    }

    public void rejectApplication() {
        for (Application each : appList) {
            approvalCounter = each.getApprovalcount();

            if (approvalCounter == 0) {
                infoZero();
            } else {
                approvalCounter--;
                each.setApprovalcount(approvalCounter);
                appRef = em.merge(each);
                em.persist(appRef);
            }

        }
    }

    public void createApplication() {
        for (Application each : appList) {
            em.persist(each);
        }
    }

    public void deleteApplication() {

        for (Application each : appList) {
            appRef = em.merge(each);
            em.remove(appRef);
        }
    }

    public void infoSix() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "This Application Already Has 6 Approvals"));
    }

    public void infoZero() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "This Application Is Already at 0 Approvals"));
    }

    public void onCellEdit(CellEditEvent event) {
        Object AL = event.getNewValue();
        Object ALO = event.getOldValue();
        //appRef1 = appList.get(0);
        //appRef1.setAl(AL);
        //appRef = em.merge(appRef1);
        //em.persist(appRef);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Application Successfully Changed"+AL+ALO));

    }
    /*public void createApplication(Application appRef){       
     em.persist(appRef);
     }  */
}

我希望有人遇到同样的问题并知道解决方案

1 个答案:

答案 0 :(得分:1)

在我看来,你的bean需要一个范围才能正常工作。 如您的代码所示,它将具有对JSF没有帮助的依赖范围。