Read / Write XPages with Managed Beans After Posting

Moving to XPage development with Java only. I successfully built XPage with the form and bound all the user interface components to a managed bean. I created a save method in a bean. I can send XPage, the document will be created, and the browser will display XPage after a full update with the current values โ€‹โ€‹in the bean. If I click the "Save" button, a new document is always created, the first document created was never updated.

My question is, what is the correct way to map XPage to the current document so that the bean always updates the document, rather than always creating a new one?

Do I have to create a bean member document, so when I save the document for the first time, do I have a handle in it? Does this mean that I do not recycle the doc object after creation?

Do I have to search for a document every time I save unid? Performance issues?

Does anyone have a good programming pattern? Using Notes 9, a data source defined in XPage.

+4
source share
4 answers

You cannot have a member of the document, because it is not serializable, and the bean should be (depending on its scope and NSF save options). In addition, after the request is completed, the document becomes invalid, which means that you cannot safely save it in a bean. Your bean should have a link to the Document so that it can load / save it on demand, for example, when you call the save () method. If your bean creates a copy of the data, you should process () the document as soon as possible, usually after loading the data and after saving it. A bean is not notified when it goes out of scope or when a request is executed. Therefore, he cannot safely manage the life cycle of the resources he holds. A great data source for viewing the ObjectData ID in the extension library. This handles all the lifecycle management for the object (Java or JavaScript), so you can focus on business logic.

+4
source

Well, there are a few things here.

First, I completely agree with the concept of switching to a real MVC pattern using Java. I am currently working on a project where the data model is approx. 30 objects where I try to implement the correct MVC pattern. I had a lot of inspiration from a series of 5 articles from Pipalia. First: http://www.pipalia.co.uk/notes-development/rethinking-xpages-part-one/ - and you can find the following 4 on your site.

Basically, I have an XPage that only refers to a View bean (which is a managed bean - a session). The bean view uses a facade layer to perform data operations (other names: CRUD, service). However, the facade layer does not directly talk to the database. It uses an interface to define a DAO object (data access object), which is an implementation of Domino data access. This is the only class that knows how to talk to the Domino database. He will get a view of the database, view and document - read the data in the Data bean (which just contains fields with setters and getters), and then delete these Domino objects again. After reading the data, they live in the Data bean in memory. Only if I change the data, I will need to call the facade layer to check and save (through the DAO layer).

In XPage, I will use EL to connect the field to the data, using something like: ViewBean.person.name (assuming the bean was a ViewBean and its bean was human and the field was a name). Then I control whether I create an object of a new person in a bean view by creating an object of an empty person.

As a side note, I decided to use the OpenNTF Domino API, so I donโ€™t have to worry about processing and have a much more modern Java implementation (for example, use maps to create new documents and improve iterators, etc.).)

/ John

+1
source

Based on what Philip said about the bean life cycle, I think you could better approach using a plain old Java object (POJO). Then you can control when the bean is created. It works the same way, in addition, you must create and process it yourself.

In my project, I had the opposite problem when I needed several documents, but I could only create it using a managed bean. When I changed it to POJO, it did a great job. I controlled the creation of a new object.

I created an object in SSJS as described below. It works great and makes me think that in the future I can just do it this way.

var s = new com.mycompany.Shipper(requestScope.field1, requestScope.field2, requestScope.field3, requestScope.field4, UNID1, UNID2); s.saveShipper(POdata); 
0
source

If I understand correctly, the reason why he creates a new document every time is because your bean has no idea which document he is dealing with. With the dominoDocument data source in XPage, this is handled by the action and documentId properties of the data source. Therefore, calling the save method in this data source not only saves the backend database, but also updates these actions and the documentId properties of the data source. As a result, any future action knows to which document it should act, since documentId was changed from an empty line to the UNID of the created backend document. Also, the action has been changed from "createDocument" to "editDocument".

If you use a bean, your save method should mimic this process by setting the variables for which the document should act. Then your save method should check if this property has a value, and in this case get the corresponding document, otherwise create a new one. Otherwise, your bean can only create documents, it will never be able to update them.

If you are not using an xe: dataObject, you should also not use a button like submit. beans do not know what to do with it. John will be a beer to confirm, but I donโ€™t know if MVC will know what to do with it. xe: dataObject has a specific saveObject property, as it is. Itโ€™s easier to just use a regular button and on the Events tab point it to the appropriate save method.

Using beans, xe: dataObject or MVC, you still need to specify the code when creating the document and when / how to get it for updating.

0
source

Source: https://habr.com/ru/post/1500751/


All Articles