tags:

views:

387

answers:

3

I have this code for a backing bean:

@PostConstruct
 public void refreshData()
 {
  rows  = (int) dd.getRows();
  pages = dd.getPages();
  getRender();
 }

// action
 public void getCount(String sql, Object... values)
  throws Exception
 {
  dd.getCount(sql, values);
  rows  = (int) dd.getRows();
  pages = dd.getPages();
 }

 // getter methods
    public boolean getRender() {
        System.out.println("pages: "+pages);
     boolean rendered = pages > 0? true: false;
     return rendered;
    } 

 public int getRows() {
  return rows;
 }
    public int getPages() {       
     return pages;
    }

Does the refreshData() method with the @PostConstruct directive get executed after or before all the getter methods? I ask this because I notice the getRender() method always return zero even though the getPages() returns a number like 10 for example.

+1  A: 

I have no idea what you mean with "before all the getter methods". At least the @PostConstruct is called immediately after the construction of the bean and the setting of all managed properties (the bean properties which are definied in faces-config.xml).

Thus roughly:

  1. Bean is constructed.
  2. Managed properties are set.
  3. @PostConstruct is called.
  4. Bean is brought in JSF lifecycle.

Your problem is likely that the value is been overriden by something else. Just run the debugger or have your code reviewed by an expert.

BalusC
+1  A: 

The JSF 1.2 spec says specifically:

Methods on managed beans declared to be in request, session, or application scope, annotated with @PostConstruct, must be called by the JSF implementation after resource injection is performed (if any) but before the bean is placed into scope.

(More details in the spec.)

McDowell
A: 

@BalusC and @McDowell: Thanks both for replying so promptly. In my haste in copy+paste, I forgot to include these declarations which appear at the top of the backing bean:

private int pages = 0;
private int rows = 0;

Would this declaration caused the getters to always return zero when the bean complete it's cycle as per BalusC dot points above? Please advise.

icepax
I believe it will depend entirely on what `dd` does. The `@PostConstruct` invocation of `refreshData` will be called before `getCount`, which seems to populate `dd`. _FYI: please edit the question in future instead of adding an answer - this is not a forum and this info may get lost in the noise._
McDowell
Thanks for the tip. I apologise but I couldn't find the Edit link in my original post. Anyway, dd is a class which has:conn = dao.getDataConnection();ps = dao.prepareStatement(conn, sql, values);rs = ps.executeQuery(); if (rs.next()) { rows = rs.getDouble(1); // returns 100 rows}And the other two methods:public double getRows(){ return rows;} public int getPages() { dpages = Math.ceil(rows/rowsPerPage); pages = (int) dpages; // returns 10 pages of records each return pages;}
icepax