问题
i'm using jsf 2.0.2 + richfaces 3.3.3. what can i do so my getter won't be invoked multiple time ??
i have this:
@ManagedBean(name = "mybean")
@SessionScoped
public class mybean implements Serializable {
public MyClass getMyClass() {
if (FacesContext.getCurrentInstance().getRenderResponse()) {
myClass = get_it_from_database();
}
return myClass;
}
i also used this:
@ManagedBean(name = "mybean")
@SessionScoped
public class mybean implements Serializable {
public MyClass getMyClass() {
if (myClass = null) {
myClass = get_it_from_database();
}
return myClass;
}
but what i wanted is to "refresh" ONCE my data whenever i refresh the page...
回答1:
You can't prevent that. That's the nature of a getter. A getter method is intented to return data (read: to provide an access point for outside), not to load data. There you normally use the bean constructor, @PostConstruct
or action method for.
To solve your particular problem, redeclare the bean to be request scoped and move the line myClass = get_it_from_database();
to bean's constructor or a @PostConstruct
method if it has injected dependencies. Or if you really insist in keeping it session scoped (you should prefer having two beans: one session scoped for session scoped data and one request scoped for request scoped data), then your first approach is the most reasonable.
See also:
- Why JSF calls getters multiple times?
回答2:
make a myclass property with getter setter and return that so on referesh it will be maintained and ha use also null check as mentioned in question. Have a nice day
来源:https://stackoverflow.com/questions/3387136/preventing-multiple-getter-invoke-in-a-session-managed-bean