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 th开发者_如何学JAVAe getPages() returns a number like 10 for example.
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:
- Bean is constructed.
- Managed properties are set.
- @PostConstruct is called.
- 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.
The JSF 1.2 spec says specifically (Page 11, Item 119 preface):
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.)
精彩评论