2015-10-15 115 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

不客气;-) –