繁体   English   中英

接缝动作方法执行两次?

[英]seam action method executed twice?

我目前正在从事接缝项目,但遇到了问题。

我刚刚创建了一个新页面(一个名为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. 显示了我的输入框和命令按钮,而不显示rich:panel,因为我的showDetails布尔值为false。
  2. 当showDetails布尔值设置为true时,将显示面板,并且迭代调用我的操作方法findRecords()。 到现在为止还挺好!
  3. 但是当我再次单击动作按钮时,动作方法findRecords()被执行两次。 这是我的问题

为什么要执行两次? 如何将其限制为一次? 现在,它花费了我们很多性能。

r

短剑

该方法最有可能在视图重建(还原视图阶段)期间和再次渲染(渲染响应阶段)期间执行。

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

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

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

<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