单击命令按钮时,Primefaces dataTable 调用方法多次.为什么? [英] Primefaces dataTable call method multiple times when click commandButton. why?

查看:22
本文介绍了单击命令按钮时,Primefaces dataTable 调用方法多次.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<p:commandButton value="Get Name List" update="nameinfo"/>
<p:dataTable id="nameinfo" var="nam" value="#{namefinder.dofind}"></p:dataTable>

  • "namefinder" 是托管 bean [@ManagedBean(name = "namefinder")]
  • dofind"是查找名称列表的方法,它以名称查找器类对象列表的形式返回值

    • "namefinder" is managed bean [ @ManagedBean(name = "namefinder")]
    • "dofind" is the method which find the name list and it return the value as namefinder class object list

      问题是:dofind"方法被调用了 7 次.

      Problem is: "dofind" method is call 7 times.

      为什么要这样做?

      推荐答案

      因为这是 JSF 的工作方式,而且根据 Java Bean 原则,这是正确的.可以根据调用者的意愿多次调用 Getter.

      Because this is the way JSF is working, and this is correct according to Java Bean principles. Getter can be called multiple times, as much as the caller wishes.

      p:dataTablevalue 属性需要 getter 方法并多次调用该方法.你不能保证一个 getter 会被调用多少次.相反,你不应该在 getter 方法中做任何逻辑.

      The value attribute of p:dataTable expects getter method and is calling that method multiple times. You can't guarantee how many times will a getter be called. Instead, you should do no logic in getter method.

      相反,提供将由您的 p:commandButton 调用的方法并刷新那里的集合.dofind 应该是带有行列表的JavaBean的字段,不应该在那里做任何逻辑.

      Instead, provide the method that will be called by your p:commandButton and refresh the collection there. dofind should be the field of JavaBean with the list of rows, no logic should be done there.

      这篇关于单击命令按钮时,Primefaces dataTable 调用方法多次.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆