0

I have an xpage where I want to have the user enter four input fields and then create a new document, and immediately have the document show in a dojo data grid on the same page. I am using managed beans for all backend data. I have one bean for the main document, and a second bean for each related data. I will tie them together using the UNID. The input fields are all bound to the shipperBean.

My issue is that the parent and only the first sub document is created. It seems like I need to instantiate a new bean, but I thought that was done for you, as in 'managed' for you.

In my button I have the following SSJS:

var POdata:NotesDatabase = session.getDatabase(database.getServer(), "PO\\PO-data");
lineItemBean.saveLineItem(POdata);  //MUST save line item in order to tie Shipper to Line Item
var liUNID = lineItemBean.getThisUNID();
var poUNID = lineItemBean.getParentUNID();
shipperBean.saveShipper(POdata, liUNID, poUNID);
shipperBean.deleteCurrentShipper();

The button performs a partial refresh on the table holding the input. The saveShipper() method saves the bean to a new document in a different nsf. The deleteCurrentShipper() method simply sets the instance vars to null. This causes them to be wiped out and ready for the next document. The front end correctly is updated to reflect the wiping out of the values. Any entering of values after the first time not saved to a new document, although they are cleared each time on the front end.

Do I need to create a new instance each time, or can I reuse the same instance like I am attempting?

Should I use a POJO instead and create it myself?

Should I not use SSJS and do it all in another java method and call that?

Should I make the second bean a property of the first?

Is there something simple I am missing?

4

3 回答 3

1

If you define a "managed" bean in faces-config.xml then it means it gets instantiated on first usage automatically. But, it will exist only one instance within scope you defined (e.g. session). Every usage of the managed bean refers then to this one instance.

In your example it means that lineItemBean is created only at first time pressing button and then same instance is used for every next button click.

I think in your case it is better to go this way:

  • create a List<lineItemBean> within your shipperBean
  • add a new lineItemBean to the list in method saveShipper() and create documents in database there
  • add a method getAllLineItems() which returns List<lineItemBean> for providing data to your data grid
于 2013-07-09T21:23:34.150 回答
1

I would suggest not using a managed bean as it does not seem to add much value to your process. In fact trying to reset the bean and its contents can cause issues to occur. Instead, define a series of java classes and use an Object Data source that is scoped to the anticipated lifecycle (usually view or request). The create method will then establish a new instance of the parent class each time a new "object' is being created. Try and have as little business logic encoded in the SSJS. The purpose of the SSJS should be to bind the front end (XSP code) to the back end (java classes). i.e. when I click on button A it should trigger a single method call applicable to the parent class. Create additional (child) classes to encapsulate different objects. As Knut has suggested us a collection of classes if you have a many-to-one relationship. The save method of your parent class can then trigger saves of the child classes back into Notes documents.

于 2013-07-10T01:42:26.333 回答
1

I would suggest to use this approach: http://www.mindoo.de/web/blog.nsf/dx/16.07.2009095816KLEBCY.htm

Your bean should implement Map interface and in get() method provide child objects according to key parameter. This way you can have very simple and "nice" bindings.

Let's say you have "table" bean providing objects "line" (child objects are not defined as beans in faces-conig.xml!). In table's get() method you will instantiate and cache, or retrieve from cache, proper line object. EL for some cell is "#{table[row_key].attribute}".

于 2013-07-11T08:29:34.177 回答