简体   繁体   English

接缝动作方法执行两次?

[英]seam action method executed twice?

I'm currently working on a seam project and have a problem. 我目前正在从事接缝项目,但遇到了问题。

I just created a new page (an xhtml called MyPage.xhtml). 我刚刚创建了一个新页面(一个名为MyPage.xhtml的xhtml)。 In xhtml my code you can find a command button and aa:repeater to display my data: 在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>

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. 显示了我的输入框和命令按钮,而不显示rich:panel,因为我的showDetails布尔值为false。
  2. When the showDetails boolean is set to true the panel is shown and the iterate calls my action method findRecords(). 当showDetails布尔值设置为true时,将显示面板,并且迭代调用我的操作方法findRecords()。 So far so good! 到现在为止还挺好!
  3. But when I click my action button again the action method findRecords() is executed twice. 但是当我再次单击动作按钮时,动作方法findRecords()被执行两次。 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, r

Dirk 短剑

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 ) 通常要注意的是,仅在需要时(缓存结果)或在调用应用程序阶段(通常使用actionactionListener ),才应执行数据库查询之类的昂贵操作

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM