<p:roweditor display =“”new =“”values,=“”even =“”update =“”db =“”was =“”not =“”perform =“”successfully =“”

时间:2016-03-15 12:00:09

标签: jsf jsf-2 primefaces

=“”

我的网络应用程序是基于Spring boot + JSF和Primefaces构建的。

要更新我的dataTable,我使用的是roweditor primefaces元素。 这是我的dataTable:

        <p:dataTable id="table" var="categoryLevel"
            value="#{cardCategoryLevelController.categoryLevels}"
            editable="true">
            <p:ajax event="rowEdit"
                listener="#{cardCategoryLevelController.onRowEdit}"
                update=":categoryLevelForm:msgs" />
            <p:ajax event="rowEditCancel"
                listener="#{cardCategoryLevelController.onRowCancel}"
                update=":categoryLevelForm:msgs" />
            <p:column headerText="Id">
                <h:outputText value="#{categoryLevel.id}" />
            </p:column>
            <p:column headerText="Category Name">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{categoryLevel.cardCategory.name}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:selectOneMenu id="category"
                            value="#{categoryLevel.cardCategory}"
                            style="width:125px">
                            <f:selectItems value="#{cardCategoryLevelController.categories}"
                                var="category" itemLabel="#{category.name}"
                                itemValue="#{category}" />
                                <f:converter binding="#{categoryConverter}"/>
                        </h:selectOneMenu>
                    </f:facet>
                </p:cellEditor>
            </p:column>
            <p:column headerText="Level Name">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{categoryLevel.level.name}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:selectOneMenu id="level"
                            value="#{categoryLevel.level}"
                            style="width:125px">
                            <f:selectItems value="#{cardCategoryLevelController.levels}"
                                var="level" itemLabel="#{level.name}" itemValue="#{level}" />
                            <f:converter binding="#{levelConverter}"/>
                        </h:selectOneMenu>
                    </f:facet>
                </p:cellEditor>
            </p:column>
            <p:column headerText="Card Drop Rate">
                <h:outputText value="#{categoryLevel.cardDropRate}" />
            </p:column>
            <p:column headerText="Created Date">
                <h:outputText value="#{categoryLevel.createdDate}">
                    <f:convertDateTime type="time" pattern="MM-dd-yyyy HH:mm:ss" />
                </h:outputText>
            </p:column>
            <p:column headerText="Updated Date">
                <h:outputText value="#{categoryLevel.updatedDate}">
                    <f:convertDateTime type="time" pattern="MM-dd-yyyy HH:mm:ss" />
                </h:outputText>
            </p:column>
            <p:column headerText="Actions">
                <p:rowEditor style="float:left; padding:5px;"/>

                <p:commandButton styleClass="no-btn" icon="ui-icon-trash"
                    rendered="#{not empty categoryLevel}"
                    action="#{cardCategoryLevelController.delete(categoryLevel.id)}"
                    update="table">
                    <p:confirm header="Confirmation"
                        message="Are you sure you want to delete this card category level"
                        icon="ui-icon-alert" />
                </p:commandButton>
            </p:column>
        </p:dataTable> 

和roweditor方法:

public void onRowEdit(RowEditEvent event) {
        CardCategoryLevel cardCategoryLevel = (CardCategoryLevel) event.getObject();
        cardCategoryLevel = cardCategoryLevelService.update(cardCategoryLevel);
        if (cardCategoryLevel != null) {
            FacesMessage msg = new FacesMessage("Card Category Level Edited", cardCategoryLevel.getId().toString());
            FacesContext.getCurrentInstance().addMessage(null, msg);

        } else {
            FacesMessage msg = new FacesMessage("Such card category level already exists");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

问题是当我编辑一些dataTable行,并且我的数据库上的更新未成功执行时,更新的行上会显示错误的值(而不是旧的值)。

我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

很好的解决方法是实现自定义验证器,它在后端逻辑之前调用。如果验证失败,数据表行将变为红色,并显示相应的消息。