Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

jsf - Primefaces dataTable call method multiple times when click commandButton. why?

<p:commandButton value="Get Name List" update="nameinfo"/>
<p:dataTable id="nameinfo" var="nam" value="#{namefinder.dofind}"></p:dataTable>
  • "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

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

    why doing like this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...