接缝动作方法执行两次?
我目前正在做一个接缝项目,遇到了一个问题。
我刚刚创建了一个新页面(一个名为 MyPage.xhtml 的 xhtml)。 在 xhtml 我的代码中,您可以找到一个命令按钮和 aa:repeater 来显示我的数据:
<!-- input fields that are filters for my table shown below -->
<h:commandButton value="View details" action="/MyPage.xhtml"/>
<rich:panel rendered=#{myAction.showDetails}>
<a:repeat value="#{myAction.findRecords()}" var="record">
<!-- Some of my code to display a table, nothing fancy -->
</a:repeat>
</rich:panel>
在我的操作中,我有这样的:
@DataModel
private List<MyEntity> records = new ArrayList<MyEntity>();
public List<MyEntity> findRecords() {
//Do some query
Query query = entityManager.createNamedQuery("myEntityQuery");
records = query.getResultList();
return records;
}
这就是页面的工作方式:
- 显示我的输入框和命令按钮,而不是我的 showDetails 中的 rich:panel布尔值是假的。
- 当 showDetails 布尔值设置为 true 时,将显示面板,并且迭代调用我的操作方法 findRecords()。到目前为止,一切都很好!
- 但是,当我再次单击操作按钮时,操作方法 findRecords() 会执行两次。这是我的问题...
为什么要执行两次?我怎样才能限制它一次?现在它花费了我们很多性能......!
克尔,
德克
I'm currently working on a seam project and have a problem.
I just created a new page (an xhtml called MyPage.xhtml).
In xhtml my code you can find a command button and a a:repeater to display my data:
<!-- input fields that are filters for my table shown below -->
<h:commandButton value="View details" action="/MyPage.xhtml"/>
<rich:panel rendered=#{myAction.showDetails}>
<a:repeat value="#{myAction.findRecords()}" var="record">
<!-- Some of my code to display a table, nothing fancy -->
</a:repeat>
</rich:panel>
in my action I have this:
@DataModel
private List<MyEntity> records = new ArrayList<MyEntity>();
public List<MyEntity> findRecords() {
//Do some query
Query query = entityManager.createNamedQuery("myEntityQuery");
records = query.getResultList();
return records;
}
This is how the page works:
- my input boxes and command button are shown, not the rich:panel as my showDetails boolean is false.
- When the showDetails boolean is set to true the panel is shown and the iterate calls my action method findRecords(). So far so good!
- But when I click my action button again the action method findRecords() is executed twice. And here's my problem...
Why should it be executed twice? How can I limit it to once? Now it costs us a lot of performance..!
Kr,
Dirk
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该方法很可能在视图重建期间(恢复视图阶段)和再次渲染期间(渲染响应阶段)执行。
尝试缓存该方法是否已经执行或仅在单击按钮时执行,并提供一个简单的 getter 来传递数据。
一般而言,您应该仅在需要时(缓存结果)或在调用应用程序阶段(通常使用
action
或actionListener
)执行昂贵的操作,例如数据库查询Most probably the method is executed during view reconstruction (Restore view phase) and during rendering again (Render response phase).
Try to cache whether the method has been executed already or execute it only when clicking the button and provide a simple getter to deliver the data.
As a general note you should do costly operations like database queries only when needed (cache results) or during the invoke application phase (generally using
action
oractionListener
)我会做这样的事情,并使用范围来存储数据。如果您在重复中使用类似的函数,它将访问重复中每一行的方法。
i Would do something like this instead and use the Scopes to store the data. if you use a function like that in repeat it will access the method for each row in the repeat.