• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple best practice question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a userDetails screen based on a managed session scope UserBean. I have created a simple outputLink on the Manager Id label to go to a managerPlusStaff screen :



The managerPlusStaff.jsp is based on a managed request scope BeanManagersStaff bean. The constructor for BeanManagersStaff populates the bean properties (manager+staff) by accessing the session scope UserBean to get the Manager Id.

This all works fine and I can see from a trace that the BeanManagersStaff is created in the Render_Response(6) step. BUT WHY AND IS THIS THE BEST WAY.

BeanManagersStaff below:

 
steve Barf
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok 2 things :

(1) I realise I didn't got the manager properly in the BeanManagersStaff constructor, fixed below.
The question is still the same - why is the request scope BeanManagersStaff created in the Render_Response(6) phase ? Is there a better way ?



(2) The user id is also used as the password hence the database call :

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Managed bean (any scope) has a lazy initialization. It means it is created at the moment when it is required for the first time. In your case, the bean is mentioned on the page first time. So, it is Render_Response(6) phase.

When user clicks on <h:outputLink value="/Nod/pages/managerPlusStaff.jsf">, it means "non-faces request generates faces response". The short (1ph -> 6ph) lifecycle is used. On the first phase, jsf checks for the view, does not find it and immediately jumps to the 6 phase. So, 6th phase is only one phase where the activity (request for beans) might happen. (However, you can use your custom ViewHandler or PhaseListener and do something what you need if any)

About Initialization: I recommend the following approach for your case: Check the current value in the getter and invoke the creator if data is not initialized yet. I.e.:



Important:

DO NOT take the managed bean directly from the session or request scope. Like that:


Again, the managed beans use a lazy initialization. If nobody touches the bean yet, it is not added to the map. So, you can get null or object. It depends of "user" has been referenced previously or has not.

Use valueBinding or variableResolver instead. They take the bean from the map if exists. Otherwize, the bean will be created first.
 
reply
    Bookmark Topic Watch Topic
  • New Topic