Xpages - inPlaceForms bind to separate java beans

77 views Asked by At

I love using managed java beans with xpages, but there is still one scenario, which i couldn't solve yet. I have an application, that shows some entries of a database and allows editing them with an inplace form.

enter image description here

Each form is bind to the back-end document. I want to use a java bean also for that with the ability to bind fields to values in the bean. I know, that i can do something with lists and hashmaps, but that is not the same. Or is it possible, to handle a list of objects from a class? Has someone an idea, how to handle that?

1

There are 1 answers

3
Mark Leusink On

Start by creating a simple Person.java POJO. Something like this:

public Person {
    String firstName;
   
    public getFirstName() {
      return firstName;
    }

    public setFirstName(String to) {
      this.firstName = to;
    }

    //etcetera...
}  

Make sure that every property you want to edit has a getter & setter.

You now have 2 options:

  • The list of people is a List<Person> and when you edit one, you edit the object directly from that list.
  • When the user wants to edit an entry, you pass (for instance) the note ID of the document from the list to the inplace form, get the document, construct a Person object and use that as the binding in the form.

Assuming that the instance of the Person is called person, you can bind them to the inputs like:

<xp:inputText value="#{person.firstName} />

For the save action in the inplace form, I would create a separate PersonRepo class with a static save function that consumes the current Person object and saves it to the database:

public class PersonRepo {

  public static void savePerson( Person person ) {
    Database db = ExtLibUtil.getCurrentDatabase();
    Document doc = db.createDocument();
    doc.replaceItemValue("firstName" , firstName);
    doc.save();        
  }

}

You can then call that from the save button:

PersonRepo.savePerson(person);