2016-03-15 84 views
1

我的web應用程序構建於Spring引導+ JSF和Primefaces上。<p:rowEditor顯示新值,甚至更新db未成功執行

要更新我的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的行,對我的數據庫更新未成功執行,錯誤的價值觀(而不是舊的)顯示在更新的行上。

我該如何解決?

回答

0

不錯的解決方法是實現自定義驗證程序,在後端邏輯之前調用該驗證程序。如果驗證失敗,數據表格行變爲紅色,並顯示相應的消息。

相關問題