JSF更新数据操作
Updating data operation in JSF
我是 JSF 的新手。我正在创建一个简单的 crud 应用程序。我已经完成了添加和删除,但是更新数据存在一些问题..... 单击编辑时,bean 填充了我要更新的值。但在下一页没有显示,待编辑......
这是指向 editPage
的 link
<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>
在调试时一切正常,即数据已分配给人员对象但未显示在更新页面上
好吧,您正在尝试访问 #{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>
这会将您引导至 url,类似于:personManipulate.xhtml?idPerson=1。然后,在 #{personManipulate}
bean 进行渲染任务之前加载人:
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 未绑定,并且可以使用 urls 进行导航。如果您对在 url 中显示人员 ID 不感兴趣,那么您可以使用 Flash 作用域将它从一个 bean 传递到另一个 bean。查看链接的帖子。
另请参阅:
- View parameter when navigating to another page
- Understand Flash Scope in JSF2
我是 JSF 的新手。我正在创建一个简单的 crud 应用程序。我已经完成了添加和删除,但是更新数据存在一些问题..... 单击编辑时,bean 填充了我要更新的值。但在下一页没有显示,待编辑......
这是指向 editPage
的 link<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>
在调试时一切正常,即数据已分配给人员对象但未显示在更新页面上
好吧,您正在尝试访问 #{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>
这会将您引导至 url,类似于:personManipulate.xhtml?idPerson=1。然后,在 #{personManipulate}
bean 进行渲染任务之前加载人:
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 未绑定,并且可以使用 urls 进行导航。如果您对在 url 中显示人员 ID 不感兴趣,那么您可以使用 Flash 作用域将它从一个 bean 传递到另一个 bean。查看链接的帖子。
另请参阅:
- View parameter when navigating to another page
- Understand Flash Scope in JSF2