FORUM PLAN UPDATE
Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.



Oct 14, 2015, 12:48 PM
16 Posts

Loading data to managed bean on Xpage open?

  • Category: Managed Beans in NSF
  • Platform: All
  • Release: 9.0.1
  • Role: Developer
  • Tags:
  • Replies: 4

Assume following:

1. Managed Beans config

<faces-config>
    <managed-bean>
        <managed-bean-name>userProfile</managed-bean-name>
        <managed-bean-class>com.company.beans.UserProfile</managed-bean-class>
        <managed-bean-scope>view</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>userProfileManager</managed-bean-name>
        <managed-bean-class>com.company.model.UserProfileManager</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>

2. Notes view called "User Profiles" exists

    - from it, document opens like: http://127.0.0.1/demo.nsf/UserProfile.xsp?documentId=6EBEF305D2E9F3AEC2257EDE004129BE&action=openDocument
    - using Xpage "UserProfile.xsp"
    
3. Xpage "UserProfile.xsp" exists to display single User Profile

    - with EL mapped fields like: userProfile.username etc.

4. On "UserProfile.xsp" open, where do I put the code to load the userProfile bean data using the userProfileManager bean

    - code to get userProfile: userProfileManager.load(param.documentId);
    - the method in userProfileManager is:
    
        public UserProfile load(String uid) throws Exception {
            return getDao().load(uid);
        }

5. Setting beforePageLoad as:

    - viewScope.put("userProfile", userProfileManager.load(param.documentId));
    
    => Seems to work, but is this how I should do it? Somehow it doesn't feel right to me... I'm managing stuff (scope) that the framework should manage.

What is the correct/preferred way to load managed bean data on Xpage open for the Xpage to use in it's input fields? I must be missing something simple and silly here... :)

TIA, Jan

Oct 14, 2015, 2:30 PM
453 Posts
Should be able to call your method

userProfileManager.load(param.documentId) in an EL on the beforePage Load. Not sure I see why you have both a UserProfile bean and a UserProfileManager bean. How is you UserProfileManager Class defined? I have a managed Bean called Payments and the Payments Class looks like this:

public class Payments implements Serializable, Collection<PaymentItem> {

I then have a Java Class called PaymentItem. The PaymentItem class is NOT a managed Bean but Payments is. In my case the Payments class is a viewScope Managed Bean, where PaymentItem is just a plain old Java class. My PaymentItem class has all the basic getters and setters for the paymentItem, while the Payments Class (which I think is similar to your userProfileManager) does all the load, save, etc manipulation of the PaymentItem. 

Take a look at David Leedy's notesIn9.com episode 182 he does a whole session on this subject. Just don't think you need a managed bean for both classes, I'm far from an expert on this subject but have developed a few managed beans and you do not need to call the viewScope("UserProfile", userProfileManager.load(param.documentId)), that is the managed Beans job. Also, not sure why userProfileManager is a request and UserProfile is view. If I read it right the userProfileManager perhaps needs to be viewScope and UserProfile not a bean just a java class.

Actuall doing the viewScope("UserProfile", userProfileManager.load(param.documentId)) defeats the purpose of the managed bean. 

 

Oct 14, 2015, 2:41 PM
589 Posts
hmm

Like Bill says I'm not sure what you're doing with the userProfileManager thing...  I rarely use requestScope myself.  Suggest you try and eliminate that at least for now to avoid confusion.

 

This looks good :

  <managed-bean>
        <managed-bean-name>userProfile</managed-bean-name>
        <managed-bean-class>com.company.beans.UserProfile</managed-bean-class>
        <managed-bean-scope>view</managed-bean-scope>
    </managed-bean>

It looks like you're trying to load the userProfile into the manager object first...  I assume for caching or something but again I would avoid that...  especially with requestScope...

 

If you load the userProfile bean directly.. with something like:

 - viewScope.put("userProfile", userProfile.load(param.documentId));

then - and I'm not sure what the getDao stuff is...  but then I'd gave a load method like

public void load(String id) {

// note this is ODA syntax... not lotus.domino.* syntax  - also no editor so could be some method problems

Session session = Factory.getSession();

Database db = session.getCurrentDatabase();

Document doc = db.getDocumentByUniversalId(id)

this.loadValues(doc)

}

I'd make that a little more robust of course.. but that's the jist.  Then in the loadValues method I'd read the values from the document and assign them to private variables...  then I'd create the getters and setters... and EL should be ready to go.  Of course you need to reverse the load stuff to save back.

EL would be something like value="#{viewScope.userProfile.firstName}"

Assuming you had a method : public String getFirstName() {...}

 

 I recently did a video on NotesIn9 that might be interesting to you.  http://www.notesin9.com/2015/09/17/notesin9-182-xpages-and-java-from-start-to-finish/

Another show that might be helpful is : http://www.notesin9.com/2013/08/01/notesin9-122-working-with-java-objects-in-xpages/

good luck.

 

Oct 14, 2015, 6:45 PM
298 Posts
as David says...
the requestScope goes away immediately so you can't call it. At least make it a view scope?

In your constructor for the  UserProfile you put code that runs when the bean in initialized by the XPages engine (everytime the XPage loads for view scope). In that constructor you could call the UserProfileManager bean using code like:
UserProfileManage managerBean1 = (UserProfileManager) ExtLibUtil.resolveVariable(FacesContext.getCurrentInstance(), "userProfileManager");

Then all the methods in your bean are available.

Howard
Oct 21, 2015, 9:20 AM
16 Posts
Got it, thanks all

Been occupied with something else for a while, but got back on this today. Thank you all for helping, especially David, much appreciated. I think I got the basic wiring nailed now - and my test works.

I now have:

  • UserProfileControl - managed bean, view scope
  • UserProfile - model/business
  • UserProfileDao - model/data access
  • UserProfileValue - value object to/from dao

My motive to use this approach (separate dao/vo) is to prepare myself for being able to port applications from Domino server to some other application server. Some day I may even try that :).


FORUM PLAN UPDATE
Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.