< h:dataTable value =#{myBean.xxx}&gt ;: getXxx()被调用多次,为什么? [英] <h:dataTable value=#{myBean.xxx}>: getXxx() get called so many times, why?

查看:136
本文介绍了< h:dataTable value =#{myBean.xxx}&gt ;: getXxx()被调用多次,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 dataTable 的简单代码。 CentralFeed 是SessionScoped Bean, PostComment 是RequestScoped Bean

Simple piece of code about dataTable. CentralFeed is SessionScoped Bean, and PostComment is RequestScoped Bean

<h:form id="table">
    <h:dataTable value="#{CentralFeed.profileComments}" var="item">
        <h:column>
            <h:outputText value="#{item.comment}"/><br/>
            <h:inputTextarea value="#{item.newComment}" rows="2"/><br/>
            <h:commandButton value="Post" action="#{PostComment.postReply(item)}" />
        </h:column>
    </h:dataTable>
</h:form>

里面 CentralFeed.java

private List<NewsFeed> profileComments = null;

public List<NewsFeed> getProfileComments() {
    PhaseId currentPhaseId = FacesContext.getCurrentInstance().getCurrentPhaseId();
    profileComments = scholarBean.findProfileCommentsByUserId(getSelectedUser().getId());
    //model = new ListDataModel<NewsFeed>(profileComments);
    return profileComments;
}

我的问题是 getProfileComments()被调用了很多。 currentPhaseId 会告诉我们该方法被调用了什么阶段。当页面首次加载时, getProfileComment 5次之间调用,在阶段6 - RENDER_RESPONSE 。该页面有一个 inputTextarea ,所以我输入一些东西,然后点击 Post (commandButton)。然后 getProfileComment 获得另一个 12次,经历阶段1-> 4。每个阶段都会调用此方法 3-4次。然后,属性 newComment 的setter方法得到调用(所以setNewComment()获取调用), getProfileComment 阶段5再次呼叫。然后 postReply()获取调用,然后 getProfileComment 再次呼叫另一个 5次 阶段6 。到底是怎么回事?是想这样吗?如果您通过我的EJB scholarBean 查看我的 getProfileComment ,我实际上查询数据库,所以不得不查询数据库像这样的20次,这是一个很糟糕的主意。

My problem is that getProfileComments() get called a lot. currentPhaseId will tell us at what phase does the method got called. When the page first load, getProfileComment get call around 5 times, at phase 6 - RENDER_RESPONSE. The page have a inputTextarea, so I type in there something, and click Post (the commandButton). Then getProfileComment get called another 12 times going through phase 1->4. Each phase call this method 3-4 times. Then after that, the setter method of the attribute newComment get call (so setNewComment() get call), the getProfileComment get call again at phase 5. Then postReply() get call, then getProfileComment get call again for another 5 times at phase 6. What is going on? Is it suppose to be like this? If you look at my getProfileComment, via my EJB scholarBean, I actually query the database, so having to query the database like 20 times like this is a very bad idea.

推荐答案

是的,在请求期间可以多次调用getter。只要它的唯一工作正常,它不会有害:返回bean属性。但是,在您的示例中,您将直接在getter方法中加载列表!应该避免这种情况。模型的初始化/加载应该放在bean的构造函数或$ code> @PostConstruct 或任何基于事件的方法,如action方法。他们只叫一次。吸烟者应该只返回模型数据,而不需要任何其他的东西(除了一些琐碎的日志记录或懒惰的加载)。

Yes, getters can be called multiple times during a request. It doesn't harm as long as it does its sole job properly: returning the bean property. However, in your example you're loading the list straight in the getter method! This should be avoided. Initializing/loading of the model should go in bean's constructor or @PostConstruct or any event based methods like the action method. They get called only once. The getters should only return model data and nothing more (apart from some trivial logging or lazy loading).

这篇关于&lt; h:dataTable value =#{myBean.xxx}&gt ;: getXxx()被调用多次,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆