3

I am trying to initialize a backing bean on page load. Already looked for many solutions, but most of them are using links,buttons or not having parameters at all.

scenario: A customer logins to a site and then should see all of his data on the following page.

backing beans:

  1. loginDetailsController - to support login process. Can access user's password and username. Backing bean for Login.xhtml.

  2. customerController - to access the rest of user details. It also has a customer_id field. It is the Backing bean for Home.xhtml. It is possible to initialize customerController entirely from customer_id.

On Home.xhtml it is possible to get customer_id this way:

 #{loginDetailsController.customer_id}

Is there a way to initialize customerController with the above value on page load?

Regards,

Daniel

Daniel
  • 754
  • 1
  • 11
  • 25

3 Answers3

11

JSF 2.0 has preRender support, that should fix your problem.

JSF 2 includes a PreRenderViewEvent that is fired after view parameters have finished processing, but before the view is rendered. The pre-render view listener can be registered using the tag, eg:

<f:metadata>
  <f:viewParam name="foo" value="#{bean.foo}"/>
  <f:event type="preRenderView" listener="#{bean.doSomething}"/>
</f:metadata>
Tarun Sapra
  • 1,851
  • 6
  • 26
  • 40
  • Thanks. Still don't understand how to pass parameters from one backing bean to another. Maybe I missed something. – Daniel Jun 14 '11 at 10:04
4

What is the scope of your beans? In any case, a method marked with @PostConstruct annotation will be called once while your bean is created.

@ManagedBean
@ViewScoped
public class LoginDetailsController() {
  private String customer_id;
// getters and setters for customer_id

  @PostConstruct
   public void init() {
      this.customer_id = fetch_the_value_from_somewhere;
    }
}

If you want your page to always execute this method, irrespective of whether the bean is already created or not, use PreRenderViewEvent as Tarun rightly suggested. But beware that this event is fired everytime page is rendered; even when user refreshes the page.

If you want to utilize this value from LoginDetailsController in customerController, you can inject it as follows

@ManagedBean
@ViewScoped
public class CustomerController() {
  @ManagedProperty(value="#{loginDetailsController}")
  private LoginDetailsController loginDetailsController;
  private String customer_id;
// getters and setters for customer_id

   @PostConstruct
   public void init() {
      this.customer_id = loginDetailsController.getCustomer_id();
    }
Niks
  • 4,802
  • 4
  • 36
  • 55
  • Both are session scoped. I get `loginDetailsController for managed bean customerController does not exist.`. Can you please write how the JSF should look like? – Daniel Jun 14 '11 at 10:57
  • It's the same code, but then `@SessionScoped` instead of `@ViewScoped`. – BalusC Jun 14 '11 at 11:27
  • Ohh I Guess you are trying to use #{loginDetailsController.customer_id} on your page whose backing bean is CustomerController. Well you may not have defined getter and setters for loginDetailsController.(even the code above does not contain that). Hence the error. You should be referring to the #{customerController.customer_id} property rather than #{loginDetailsController.customer_id} – Niks Jun 14 '11 at 12:08
1

This is how I solved it:

Because LoginDetailsController is session scoped - customer_id is always initialized.

So - I added

@ManagedProperty(value="#{loginDetailsController.customer_id}") 
private String customer_id;

Plus - getter and setter for customer_id.

On LoginDetailsController, as Nikhil said - with a little change. Then, it is possible to use #{customerController.customer_id} and the rest of customerController initialized elements of customerController on Home.xhtml.

Thank you all!

Daniel
  • 754
  • 1
  • 11
  • 25