0

I am trying to implement CRUD functionality in JSF/CDI.

I am stuck trying to implement edit functionality. I have an xhtml page that displays a list of Person entities in a datatable.

I want to place an edit link in the datatable for each person but I have completely forgotten how some of the mechanisms in JSF/CDI work. How do I pass the Person entity in the datatable to the Java code .?

do i do something like :

personList.xhtml

<h:datatable value="#{personList}" var="person">
    <h:commandButton value="Go to edit page" action="#{personController.editPerson(person)"} />
</h:datatable>

personController.xhtml

@Named
@RequestScoped
public class PersonController {

    //Person is a JPA entity so I can't inject it
    Person editPerson;    

    public String editPerson(Person person) {
      editPerson = person;
      return "editPerson";
    }

    public String saveChanges(){
      entityManager.merge(editPerson);
    }

}

editPerson.xhtml

<h:form method="post">
    <h:inputText value="#{personController.editPerson.name}" />
    <h:commandButton value="Save Changes" action ="#{personController.saveChanges()}" />
</form>

After reading the link provided by BalusC I wanted to ask if there is not a better way than using a converter in conjunction with the tag to pass through the id?

I come from a Seam 2 background where Seam would automatically define the converters.Additionally that post doesn't make use of any CDI functionality...just Managed Beans..

  • As to conversion, just use/create a generic converter. This has also been asked and answered before. Alternatively, use POST instead of GET as you apparently did before (but then you end up with SEO and UX problems -which is in turn unrelated to JSF by the way). As to bean management framework, there's absolutely no difference in JSF approach depending on whether you manage beans by JSF, CDI, Spring or whatever. It's a matter of changing the annotations. – BalusC May 19 '16 at 08:18
  • Ok thanks for your help BalusC. Think I understand enough to get up and running now. Will investigate the generic converter solution – Duran Wesley Harris May 19 '16 at 08:31
  • In Netbeans you can very easily autogenerate it all, similar to what Seam would have done. Maybe in another project just for inspiration – Jaqen H'ghar May 20 '16 at 05:40
  • Oh like Seam-gen? That's pretty cool! – Duran Wesley Harris May 20 '16 at 09:15

0 Answers0