0

In my actual project I have noticed that the method that populates the ui:repeat tag, is being invoked when there is a post call, even though the ui:repeat is not part of the submitted form.

I have been trying to check againts the jsf documentation if that is the way it should work, with no success.

Is it supposed to work this way?

Thanks in advance.

Sample code:

When the button is clicked the method anotherBean.getCollection is being invoked:

<h:form id="firstForm">  
    <h:commandButton action="#{someBean.someAction}"/>   
</h:form>

<h:form id="secondForm">  
    <ui:repeat var="product" value="#{anotherBean.populatCollection}" >  
        <!-- CODE -->  
    </ui:repeat>  
</h:form>
Sal81
  • 101
  • 1
  • 1
  • 7
  • Most likely this is JSF reconstructing the view to apply the changes from your POST. This includes the complete page, not only parts of it, AFAIK. Maybe someone can link to the correct reference part. – mabi Jan 14 '14 at 13:00
  • Hi, thanks for the anwser. So, is there a way to avoid this? Rgrds. – Sal81 Jan 15 '14 at 09:22
  • No that I'm aware of. If you perform some DB fetching in there, have `anotherBean` in a scope greater than `RequestScoped` and store the value in a field, only returning the field's value in the getter (as it should be). – mabi Jan 15 '14 at 09:38

2 Answers2

1

In first place, a getter method shouldn't be populating the value at all. A getter method should, as its name says, just return the already-populated value.

You're not terribly clear on the concrete functional requirement, i.e. when exactly did you intend to populate the value, but one way would be moving the populating logic to the @PostConstruct of #{anotherBean}.

@ManagedBean
@ViewScoped
public class AnotherBean {

    private List<Something> getCollection; // Terrible variable name by the way.

    @PostConstruct
    public void init() {
        getCollection = populateItSomehow();
    }

    public List<Something> getGetCollection() {
        return getCollection; // See, just return the property, nothing more!
    }

}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi @BalusC, Thanks for your answer. Thats true that the name I have choosen for the example code is not the best. I have change the example in order to make it more clear. Once this is said, I still wondering why "anotherBean.populateColletion()" is being invoked when the user make a post by clicking in a button that is in a different form (form id="firstForm"). Is it the way it is supposed to work? Thanks. – Sal81 Jan 17 '14 at 14:05
  • Because JSF needs to save/restore the view as well. Just do not do business logic in getter methods then you also don't need to worry about them being called multiple times at "undesired" moments. – BalusC Jan 17 '14 at 14:11
0

So, it looks like ui:repeat tag is invoking the methods assigned to its value argument when a post is done, no matter if the post is done from another form.

Thanks for the help.

Sal81
  • 101
  • 1
  • 1
  • 7