0

I'm much newbie to the JSF. I am creating a simple crud app. I have done adding, and deleting but there is some problem with updating data..... when edit is clicked, bean is populated with values of whom i want to update. but it is not shown in the next page, in which it is to be edited......

this is the link which leads to editPage

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

this is the code which assign person data to person entity

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

This is the code which updates per save person

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

}

This is the page where values should be shown and Updated

<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>

This is the navigation rule

 <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> 

On Debugging everything is fine i.e. data is assigned to person object but not shown on update page

Aritz
  • 30,971
  • 16
  • 136
  • 217

1 Answers1

0

Well, you're trying to access the #{personManipulate} bean, which still has not been created when you're in the people list. Supposing it is a @ViewScoped bean as it should, that should be created when you address to JSF/personManipulate.xhtml specifically. So I would encourage you to do the People List -> Edit Person transition using a GET request, instead:

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>

That will direct you to an url similar as: personManipulate.xhtml?idPerson=1. Then, load the person before the #{personManipulate} bean makes the rendering task:

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);
}   

That way you keep both view beans unbound and you use urls for navigation. If you aren't interested in showing the person id in the url, then you can use the Flash scope to pass it from bean to bean. Have a look at the linked posts.

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217