开发者

Initializng a Backing Bean With Parameters on Page Load with JSF 2.0 [duplicate]

This question already has answers here: 开发者_高级运维 How do I process GET query string URL parameters in backing bean on page load? (4 answers) Closed 7 years ago.

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


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>


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();
    }


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜