0

I am having a data table like below in my xhtml:

<p:dataTable var="employee"
      value="#{employeeBean.employeeLazyDataModel}">

Now in my backing bean, I have:

@ManagedBean
@RequestScoped
public class EmployeeBean implements Serializable {

    @ManagedProperty("#{" + EmployeeLazyDataModel.MANAGEDBEAN_NAME + "}")
    private EmployeeLazyDataModel employeeLazyDataModel;

    public void preRender(ComponentSystemEvent event) throws Exception {
        employeeLazyDataModel= // make a database call
    }

  // and getters + setters

Do you think I can call managedproperty inside preRender method? Please suggest. Thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sudipta Deb
  • 1,040
  • 3
  • 23
  • 43
  • 1
    Please first try if you can before you ask here – Adrian Mitev Sep 05 '13 at 06:44
  • I did that and it is working. But I don't want to do all expensive operations inside preRender. And even if I move the stuffs to PostConstruct method, then also it will be called every time because we are in RequesScope – Sudipta Deb Sep 05 '13 at 06:46

1 Answers1

1

Surely this will work. The @ManagedProperty is injected directly after bean's construction. The preRenderView event listener requires an already-constructed bean before being invoked. So this is indeed guaranteed to work.

However, the canonical approach to initialize bean's state based on managed properties is a @PostConstruct annotated method. Thus, so:

@ManagedBean
@RequestScoped
public class EmployeeBean implements Serializable {

    @ManagedProperty("#{" + EmployeeLazyDataModel.MANAGEDBEAN_NAME + "}")
    private EmployeeLazyDataModel employeeLazyDataModel;

    @PostConstruct
    public void init() throws Exception {
        employeeLazyDataModel= // make a database call
    }

    // ...
}

(don't forget to remove the <f:event> from the view)

As to the "expensive operations" complaint in the comment, just put the bean in view scope if you don't want to run it on a per-request basis, but only once on a per-view basis.

@ManagedBean
@ViewScoped
public class EmployeeBean implements Serializable {

Note that the preRenderView event is fired before the view is rendered (thus, on every single HTTP request!), so you really need to keep the @PostConstruct here. Otherwise, you have to add a check if FacesContext#isPostback() returns false.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555