• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple best practice question  RSS feed

 
steve Barf
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
  • 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 :

 
Sergey Smirnov
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • 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 <hutputLink 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!