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

查看:96
本文介绍了单击commandButton时,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.

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

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