: getXxx() 被调用了这么多次,为什么?
关于dataTable
的简单代码。 CentralFeed
是 SessionScoped Bean,PostComment
是
<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;
}
RequestScoped Bean我的问题是 getProfileComments()
得到打了很多电话。 currentPhaseId 会告诉我们该方法在哪个阶段被调用。页面首次加载时,getProfileComment
在第 6 阶段 - RENDER_RESPONSE
大约被调用 5 次。该页面有一个 inputTextarea
,因此我在其中输入一些内容,然后单击 Post
(命令按钮)。然后,getProfileComment
又被调用 12 次,经历阶段 1->4。每个阶段调用此方法 3-4 次。然后,属性的setter方法newComment
get调用(因此setNewComment() get调用),getProfileComment
get在阶段5。然后
postReply()
得到调用,然后 getProfileComment
在阶段 6
再次得到调用5 次。到底是怎么回事?应该是这样吗?如果您查看我的 getProfileComment
,通过我的 EJB scholarBean
,我实际上查询了数据库,因此必须像这样查询数据库 20 次是一个非常糟糕的主意。
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>
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;
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,在一个请求期间可以多次调用 getter。只要它正确完成其唯一的工作:返回 bean 属性,它就不会造成损害。但是,在您的示例中,您直接在 getter 方法中加载列表!应该避免这种情况。模型的初始化/加载应该在 bean 的构造函数或 @PostConstruct 或任何基于事件的方法(如操作方法)中进行。他们只接到一次电话。 getter 应该只返回模型数据,仅此而已(除了一些琐碎的日志记录或延迟加载)。
另请参阅:
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).See also: