[英]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;
}
頁面的工作方式如下:
為什么要執行兩次? 如何將其限制為一次? 現在,它花費了我們很多性能。
r
短劍
該方法最有可能在視圖重建(還原視圖階段)期間和再次渲染(渲染響應階段)期間執行。
嘗試緩存該方法是否已經執行或僅在單擊按鈕時才執行,並提供一個簡單的獲取方法來傳遞數據。
通常要注意的是,僅在需要時(緩存結果)或在調用應用程序階段(通常使用action
或actionListener
),才應執行數據庫查詢之類的昂貴操作
我會做這樣的事情,並使用范圍來存儲數據。 如果您重復使用類似的函數,它將在重復中的每一行訪問該方法。
<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.