2015-10-15 54 views
0

我對JSF很新手。我正在創建一個簡單的粗糙應用程序。我已經完成了添加和刪除操作,但更新數據時出現了一些問題.....點擊編輯時,bean會填入我想要更新的值。但它不會在未來頁,其中將被編輯顯示......在JSF中更新數據操作

這是導致editPage

<h:column> 
    <f:facet name="header">Edit</f:facet> 
    <h:form> 
     <h:commandLink value="Edit" action="#{personManipulate.editPerson(person)}"/> 
    </h:form> 
</h:column> 

的鏈接,這是其分配人數據的代碼對人實體

public String editPerson(PersonEntity person){ 
    this.person=person; 
    return "success2"; 
} 

這是代碼,每更新保存人

public String savePerson(){ 
    if(person.getPersonId() > 0){ 
     personDao.updatePerson(person); 
     return "success"; 
    }else{ 
     personDao.addPerson(person); 
     return "success"; 
    } 
} 

} 

這是其中值應顯示和更新

<h:form> 
    <h:panelGrid columns="2"> 
     <f:facet name="header">Person</f:facet>  
     <h:outputLabel for="firstName" value="First name" /> 
     <h:inputText id="firstName" value="#{personManipulate.person.firstName}" 
      label="First name" /> 
     <h:outputLabel for="lastName" value="Last name" /> 
     <h:inputText id="lastName" value="#{personManipulate.person.lastName}" 
      label="Last name" /> 

     <h:outputLabel for="address" value="Address" /> 
     <h:inputText id="address" value="#{personManipulate.person.address}" 
      label="Address" /> 

     <h:outputLabel for="phone" value="Contact Number" /> 
     <h:inputText id="phone" value="#{personManipulate.person.phone}" /> 

     <h:commandButton action="#{personManipulate.savePerson}" value="Submit" /> 
     <h:button outcome="ShowPersons.xhtml" value="Cancel" /> 
    </h:panelGrid> 
</h:form> 

該頁面的導航規則

<navigation-rule> 
    <from-view-id>JSF/personManipulate.xhtml</from-view-id> 
    <navigation-case> 
     <from-outcome>success</from-outcome> 
     <to-view-id>JSF/ShowPersons.xhtml</to-view-id> 
     <redirect /> 
    </navigation-case> 
</navigation-rule> 

<navigation-rule> 
    <from-view-id>JSF/ShowPersons.xhtml</from-view-id> 
    <navigation-case> 
     <from-outcome>success2</from-outcome> 
     <to-view-id>JSF/personManipulate.xhtml</to-view-id> 
     <redirect /> 
    </navigation-case> 
</navigation-rule> 

在調試一切細微即數據被分配到人的對象,但更新頁面上未顯示

回答

0

好,您正在嘗試訪問#{personManipulate} bean,該bean在人員列表中時尚未創建。假設它是一個@ViewScoped bean,應該在創建地址爲JSF/personManipulate.xhtml時創建它。所以,我會鼓勵你做人民名單 - 使用GET請求>編輯個人過渡,而不是:

showPersons.xhtml

<h:column> 
    <f:facet name="header">Edit</f:facet> 
    <h:form> 
     <h:link value="Edit" outcome="personManipulate.xhtml"> 
     <f:param name="idPerson" value="#{person.id}" /> 
     </h:link> 
    </h:form> 
</h:column> 

,將引導您進入一個網址類似: personManipulate.xhtml?idPerson = 1。然後,裝入人#{personManipulate}豆使得渲染任務之前:

personManipulate.xhtml

<f:metadata> 
    <f:viewParam name="idPerson" value="#{personManipulate.idPerson}" /> 
    <f:event listener="#{personManipulate.loadData}" type="preRenderView" /> 
</f:metadata> 

PersonManipulate.java

public void loadData(ComponentSystemEvent event){ 
    person = service.getPersonById(idPerson); 
} 

你保留兩個視圖bean綁定這樣並使用網址進行導航。如果你不想在url中顯示person id,那麼你可以使用Flash範圍將它從bean傳遞給bean。看看鏈接的帖子。

參見:

+0

非常感謝@Xtreme騎車 –

+0

不客氣;-) –