Java seam动作方法执行两次?

Java seam动作方法执行两次?,java,seam2,Java,Seam2,我目前正在做一个seam项目,遇到了一个问题 我刚刚创建了一个新页面(一个名为MyPage.xhtml的xhtml)。 在xhtml我的代码中,您可以找到一个命令按钮和一个:中继器来显示我的数据: <!-- input fields that are filters for my table shown below --> <h:commandButton value="View details" action="/MyPage.xhtml"/> <rich:pa

我目前正在做一个seam项目,遇到了一个问题

我刚刚创建了一个新页面(一个名为MyPage.xhtml的xhtml)。 在xhtml我的代码中,您可以找到一个命令按钮和一个:中继器来显示我的数据:

<!-- 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;
}
@DataModel
私有列表记录=新的ArrayList();
公共列表findRecords(){
//询问
Query Query=entityManager.createNamedQuery(“myEntityQuery”);
records=query.getResultList();
退货记录;
}
这是页面的工作方式:

  • 将显示我的输入框和命令按钮,而不是rich:面板,因为我的showDetails布尔值为false
  • 当showDetails布尔值设置为true时,将显示面板,迭代调用我的操作方法findRecords()。到目前为止还不错
  • 但是,当我再次单击我的操作按钮时,操作方法findRecords()会执行两次。这是我的问题 为什么要执行两次呢?我怎么能把它限制在一次呢?现在它让我们损失了很多性能

    韩国


    Dirk

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

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


    一般来说,您应该只在需要时(缓存结果)或在调用应用程序阶段(通常使用
    action
    actionListener
    )执行诸如数据库查询之类的代价高昂的操作。

    我会这样做,并使用作用域存储数据。如果使用repeat中的函数,它将访问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();
    }
    
    
    @出去
    私有列表记录=新的ArrayList();
    @工厂(value=“记录”)
    公共无效文件记录(){
    //询问
    Query Query=entityManager.createNamedQuery(“myEntityQuery”);
    records=query.getResultList();
    }