1

I am trying to trigger a method doSomething() through p:selectOneButton. Though every attempt to do so fails.

As p:selectOneButton has no actionListener I thought on using p:ajax and point it to the method doSomething(), but while it compiles without errors it still doesn't trigger the desired method when I click on it.

Here is my code sample:

<h:form id="languageSelectionForm">
    <p:selectOneButton value="#{language.selectedLanguage}">
        <p:ajax event="change" listener="#{language.doSomething}" />
        <f:selectItems value="#{language.languagesMap}" />
    </p:selectOneButton>
</h:form>

How can I trigger doSomething() when clicking on p:selectOneButton?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Socrates
  • 8,724
  • 25
  • 66
  • 113
  • Please check this http://stackoverflow.com/questions/8407183/how-do-i-get-primefaces-pselectonemenu-to-call-valuechangelistener – Subodh Joshi Mar 10 '16 at 06:02
  • check your method signature and EL combination. It sounds like they don't match, see the duplicate – Kukeltje Mar 10 '16 at 07:43
  • 1
    It works for me and anyone else in the world when copy'n'paste'n'stubbed'n'runned in a scratchpad project with everything set to bare defaults and using most recent versions of JSF/PrimeFaces/Tomcat. The cause of your problem is not visible in the information provided so far. Please work on that http://stackoverflow.com/tags/jsf/info – BalusC Mar 10 '16 at 08:38
  • 1
    @BalusC I did this example. And also i am facing same problem with `valueChangeListner`. Then i added `` and then its working. Is this possible? Because I know that `` will not work with primefaces extensions. – Hiren Mar 10 '16 at 10:51
  • You used `#{language.doSomething}"` in your `p:ajax` and you mentioned `doSomething()` as a method signature, those two did not match. WE (I at least thing BalusC did to) used `doSomething(ActionEvent) as mentioned in point 11 in the 'duplicate' I posted. So that is still weird (or your method signature is not doSomething() and we (at least I) chased ghosts. Next time indeed follow the link BalusC posted – Kukeltje Mar 10 '16 at 17:13

1 Answers1

0

I found the error!

To fill p:selectOneButton I used <f:selectItems value="#{language.languagesMap}" />. On the controller end I had a method that looked like this one:

public Map<String, LanguageSpec> getLanguagesMap() { ... }

and public a getter and public setter connected to the parameter for the selection:

private LanguageSpec selectedLanguage;

When I changed the second Map-argument from LanguageSpec to String and the same for the selectedLanguage parameter, the events perfomed and triggered doSomething().

Hence, without having checked, I assume that p:selectOneButton can only handle events when <f:selectItems value="#{language.languagesMap}" /> is filled with a Map where both the key and val are filled with String objects.

Special thanks to all the above helpers. By confirming that my example code worked when copy & pasting, I was able to test my code specifically for this issue.

Socrates
  • 8,724
  • 25
  • 66
  • 113
  • We should not be 'testing working code'... :-( – Kukeltje Mar 10 '16 at 17:15
  • As mentioned above, when posting this post I was assuming my code was wrong. I didn't know it was perfectly fine. Still, thanks a lot for your help. – Socrates Mar 10 '16 at 17:17
  • If your java method was doSomething() it WAS wrong... And if it is working, then why post it??? Sorry to say, but if you keep posting questions like this, you'll get less and less help... next time post an [mcve] – Kukeltje Mar 10 '16 at 17:24
  • The signature with `doSomething()` works perfectly fine. I don't need the `ActionEvent` as a param. – Socrates Mar 10 '16 at 17:31
  • Set JSF project state to `Development`. You should have seen a faces message with a conversion error. See also http://stackoverflow.com/tags/jsf/info – BalusC Mar 10 '16 at 17:33
  • Thanks @BalusC I'll look into that. – Socrates Mar 10 '16 at 20:00