【问题标题】:Updating data operation in JSF在 JSF 中更新数据操作
【发布时间】:2015-10-15 21:54:10
【问题描述】:

我是 JSF 的新手。我正在创建一个简单的 crud 应用程序。我已经完成了添加和删除,但是更新数据存在一些问题.....单击编辑时,bean 填充了我要更新的值。但它没有显示在下一页,要在其中进行编辑......

这是指向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> 

在调试时一切都很好,即数据已分配给人员对象但未显示在更新页面上

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    嗯,您正在尝试访问 #{personManipulate} 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} 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 未绑定,并使用 url 进行导航。如果您对在 url 中显示人员 ID 不感兴趣,则可以使用 Flash 范围将其从 bean 传递到 bean。看看链接的帖子。

    另请参阅:

    【讨论】:

    • 非常感谢@Xtreme Biker
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2012-10-18
    相关资源
    最近更新 更多