Preventing multiple getter invoke in a session managed bean?

╄→尐↘猪︶ㄣ 提交于 2019-12-24 17:27:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!