一段简单的代码关于dataTable
. CentralFeed
是 SessionScoped Bean,并且PostComment
是 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>
inside 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 times,在第 6 阶段 -RENDER_RESPONSE
。该页面有一个inputTextarea
,所以我在那里输入一些内容,然后单击Post
(命令按钮)。然后getProfileComment
被称为另一个12 times经历阶段 1->4。每个阶段都会调用这个方法3-4次。然后是属性的setter方法newComment
get 调用(因此 setNewComment() get 调用),getProfileComment
再次接到电话:phase 5
. Then postReply()
接到电话,然后getProfileComment
再次接到另一个电话5 times at phase 6
。到底是怎么回事?应该是这样吗?如果你看看我的getProfileComment
,通过我的 EJBscholarBean
,我实际上查询数据库,所以必须像这样查询数据库 20 次是一个非常糟糕的主意。
是的,在一个请求期间可以多次调用 getter。只要它正确完成其唯一的工作:返回 bean 属性,它就不会造成损害。但是,在您的示例中,您直接在 getter 方法中加载列表!应该避免这种情况。模型的初始化/加载应该在 bean 的构造函数中进行或者@PostConstruct
或任何基于事件的方法,如操作方法。他们只接到一次电话。 getter 应该只返回模型数据,仅此而已(除了一些琐碎的日志记录或延迟加载)。
也可以看看:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)