1

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?

Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76

1 Answers1

4

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

Mark Leusink
  • 3,747
  • 15
  • 23
  • 1
    I'd add something like checking if the person object is already associated to a NotesDocument and get that instead of creating a new one by db.getDocumentByUNID(person.unid)... But I assume this should be clear and your code is only an example... – Tode Nov 12 '21 at 11:56
  • Oh, looks easy. Can I use every java class, I've made? Do I have to make an entry in the faces-config.xml as managed bean for that class? – Vera Nentwich Nov 16 '21 at 10:23
  • Yes, you can call code from other classes you've made. You only need to add entries to the faces-config file if you're using managed beans, but in my example I've used none. – Mark Leusink Nov 16 '21 at 12:39