Calling a void method from a composite component

2.7k Views Asked by At

I'm trying to build a composite JSF component and I'm having some trouble calling a method. My component has a few inputs inside a and a add button responsible for adding more objects do the list that feeds the repeat. I want that button to call a different method according to the Managed Bean responsible for the page that it is responsible for (so it should be a variable set in each xhtml). But everytime I hit the button, I get the exception message:

The class 'com.test.TestMB' does not have the property 'addAction'.

This is my component:

<composite:interface>
    <composite:attribute name="id" required="false"  />
    <composite:attribute name="value" required="true"  />
    <composite:attribute name="addAction" required="true" />
</composite:interface>

<composite:implementation>
    <ui:repeat value="#{cc.attrs.value}" >      
        <!-- some inputs and other components -->
    </ui:repeat>
    <p:commandButton actionListener="#{cc.attrs.addAction}" />
</composite>

And this is my implementation:

                <cc:compositecomp id="component"
                value="#{testMB.list}"
                addAction="#{pessoaJuridicaMB.addToListMethod}" />

My TestMB class not only contains the method but also works fine if I change the button to:

<p:commandButton actionListener="#{testMB.addToListMethod}" />

The addToListMethod is something very simple:

public void addToListMethod() {
list.add(model); }

Any ideia of what is wrong and how can I fix it?

2

There are 2 best solutions below

2
Javier Haro On BEST ANSWER

You have to add the method-signature attribute to the addAction attribute in the interface definition. So replace this:

<composite:attribute name="addAction" required="true" />

by this:

<composite:attribute name="addAction" required="true" 
    method-signature="void addAction(javax.faces.event.ActionEvent)" />
4
Denis Klein On

There was something that needs to be added lametaweb's answer at least in my case and I'll explain it here in case someone else has the same problem: I was getting a wrong number of arguments exception. The thing is that an actionListener always passas an ActionEvent object as a parameter to the method it is invoking and the way lametaweb fixed my code I was telling that the method wouldn't passa any parameter. So I had to fix two thing here: declare de ActionEvent parameter in the method-signature and explicitly put that object as a recieved parameter of my void method so they would match the number of arguments sent/recieved, even if I'm not going to use the ActionEvent in my method.

The final code of the composite line had to be:

<composite:attribute name="addAction" required="true" 
    method-signature="void addAction(javax.faces.event.ActionEvent)" />

And my method

public void addToListMethod(ActionEvent event) {
   list.add(model); 
}