2

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Denis Klein
  • 101
  • 1
  • 11
  • Did you try my answer? If it resolve your issue, please accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green. – Javier Haro Sep 23 '15 at 08:42
  • Sorry for taking so long in seeing your answer... I just tried it, but it didn't work: I'm getting java.lang.IllegalArgumentException: wrong number of arguments... any idea of why? – Denis Klein Sep 23 '15 at 12:45

2 Answers2

4

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)" />
Javier Haro
  • 1,255
  • 9
  • 14
0

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); 
}
Denis Klein
  • 101
  • 1
  • 11
  • Food for read: http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener – BalusC Sep 23 '15 at 14:13
  • I answered the question too hastily. I've edited it with the correct answer. Thanks. – Javier Haro Sep 23 '15 at 15:47
  • BalusC, thanks for the reading: I'm aways reading about it and I'm still never sure of which should I use in each case (and the same for the difference between commandButton and button) – Denis Klein Sep 23 '15 at 16:36
  • lametaweb, don't worry: the idea of your answer was correct and led me to the answer (and forced me to learn a little bit more... as a begginer this is crucial for me! ;-) ) – Denis Klein Sep 23 '15 at 16:37