Initializng a Backing Bean With page loading options using JSF 2.0

I am trying to initialize a bean backup on page load. Already searched for a lot of solutions, but most of them use links, buttons or have no parameters at all.

scenario: The client logs in to the system on the site, and then should see all his data on the next page.

beans support:

  • loginDetailsController - to support the login process. Access password and username. Backup bean for Login.xhtml.

  • clientController - to access the rest of the user’s details. It also has a customer_id field. This is a backup bean for Home.xhtml. You can fully initialize customerController from customer_id.

In the Home.xhtml file, you can get client_id as follows:

 #{loginDetailsController.customer_id}

Is there a way to initialize a customerController with the above value when the page loads?

Hi,

Daniel

+3
source share
3 answers

Here's how I solved it:

Since LoginDetailsController is a session area - client_id is always initialized.

So - I added

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

Plus getter and setter for customer_id.

The LoginDetailsController, as Nikil said, with a slight change. You can then use #{customerController.customer_id}, and the rest of the initialized elements of the customerController customerController at Home.xhtml.

Thanks everyone!

+1
source

JSF 2.0 has preRender support which should fix your problem.

JSF 2 PreRenderViewEvent, , . , :

<f:metadata>
  <f:viewParam name="foo" value="#{bean.foo}"/>
  <f:event type="preRenderView" listener="#{bean.doSomething}"/>
</f:metadata>
+10

beans? , @PostConstruct bean.

@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, whether the bean is created or not, use PreRenderViewEventTarun as suggested correctly. But be careful that this event is fired every time a page is displayed; even when the user refreshes the page.

If you want to use this value from LoginDetailsControllerin customerController, you can enter 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();
    }
+4
source

All Articles