防止会话管理 bean 中的多个 getter 调用?
我正在使用 jsf 2.0.2 + richfaces 3.3.3。 我该怎么做才能避免多次调用我的吸气剂?
我有这个:
@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;
}
但我想要的是每当我刷新页面时“刷新”一次我的数据......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你无法阻止这一点。这就是吸气剂的本质。 getter 方法旨在返回数据(即:为外部提供访问点),而不是加载数据。在那里,您通常使用 bean 构造函数、@PostConstruct 或操作方法。
要解决您的特定问题,请重新声明 bean 为请求作用域,并将行
myClass = get_it_from_database();
移动到 bean 的构造函数或@PostConstruct
方法(如果它已注入依赖项) 。或者,如果您确实坚持保持其会话范围(您应该更喜欢有两个 bean:一个会话范围用于会话范围数据,一个请求范围用于请求范围数据),那么您的第一种方法是最合理的。另请参阅:
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:
使用 getter setter 创建一个 myclass 属性并返回该属性,以便在引用时将对其进行维护,并且还可以使用问题中提到的 null 检查。祝你今天过得愉快
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