接缝动作方法执行两次?

发布于 2024-12-04 17:24:40 字数 1153 浏览 0 评论 0原文

我目前正在做一个接缝项目,遇到了一个问题。

我刚刚创建了一个新页面(一个名为 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;
}

这就是页面的工作方式:

  1. 显示我的输入框和命令按钮,而不是我的 showDetails 中的 rich:panel布尔值是假的。
  2. 当 showDetails 布尔值设置为 true 时,将显示面板,并且迭代调用我的操作方法 findRecords()。到目前为止,一切都很好!
  3. 但是,当我再次单击操作按钮时,操作方法 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:

  1. my input boxes and command button are shown, not the rich:panel as my showDetails boolean is false.
  2. When the showDetails boolean is set to true the panel is shown and the iterate calls my action method findRecords(). So far so good!
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停滞 2024-12-11 17:24:40

该方法很可能在视图重建期间(恢复视图阶段)和再次渲染期间(渲染响应阶段)执行。

尝试缓存该方法是否已经执行或仅在单击按钮时执行,并提供一个简单的 getter 来传递数据。

一般而言,您应该仅在需要时(缓存结果)或在调用应用程序阶段(通常使用 actionactionListener)执行昂贵的操作,例如数据库查询

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 or actionListener)

倥絔 2024-12-11 17:24:40

我会做这样的事情,并使用范围来存储数据。如果您在重复中使用类似的函数,它将访问重复中每一行的方法。

<h:commandButton value="View details" action="/MyPage.xhtml"/>

<rich:panel rendered=#{myAction.showDetails}>
    <a:repeat value="#{records}" var="record">
        <!-- Some of my code to display a table, nothing fancy -->
    </a:repeat>
</rich:panel>



@Out
private List<MyEntity> records = new ArrayList<MyEntity>();

@Factory(value="records")
public void findRecords() {
   //Do some query
   Query query = entityManager.createNamedQuery("myEntityQuery");
   records = query.getResultList();
}

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.

<h:commandButton value="View details" action="/MyPage.xhtml"/>

<rich:panel rendered=#{myAction.showDetails}>
    <a:repeat value="#{records}" var="record">
        <!-- Some of my code to display a table, nothing fancy -->
    </a:repeat>
</rich:panel>



@Out
private List<MyEntity> records = new ArrayList<MyEntity>();

@Factory(value="records")
public void findRecords() {
   //Do some query
   Query query = entityManager.createNamedQuery("myEntityQuery");
   records = query.getResultList();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文