防止在会话托管bean中调用多个getter? [英] Preventing multiple getter invoke in a session managed bean?
问题描述
我正在使用jsf 2.0.2 + richfaces 3.3.3. 我该怎么办,这样我的吸气剂将不会被多次调用?
i'm using jsf 2.0.2 + richfaces 3.3.3. what can i do so my getter won't be invoked multiple time ??
我有这个:
@ManagedBean(name = "mybean")
@SessionScoped
public class mybean implements Serializable {
public MyClass getMyClass() {
if (FacesContext.getCurrentInstance().getRenderResponse()) {
myClass = get_it_from_database();
}
return myClass;
}
我也使用了这个:
@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...
推荐答案
您无法阻止.那就是吸气剂的本质.一种getter方法旨在返回数据(阅读:为外部提供访问点),而不是加载数据.在那里,您通常使用bean构造函数,@PostConstruct
或action方法.
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.
要解决您的特定问题,请重新声明要请求范围的bean,并将行myClass = get_it_from_database();
移动到bean的构造函数或@PostConstruct
方法(如果它已注入依赖项).或者,如果您真的坚持让它保持会话范围(您应该更喜欢使用两个bean:一个会话范围为会话范围的数据,另一个请求范围为请求范围的数据),那么您的第一种方法是最合理的.
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.
这篇关于防止在会话托管bean中调用多个getter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!