Attaching Multiple ActionListeners to p:menuitem

UI Components for JSF
Post Reply
kwrobel
Posts: 12
Joined: 17 Jan 2013, 23:02
Location: Chicagoland, Illinois, U.S.A.
Contact:

08 May 2015, 04:13

As the subject says: I'd like to be able to attach multiple action listeners to a <p:menuitem>, similar to how it is possible to attach multiple action listeners to a <p:commandbutton>. This is usually accomplished by adding the <f:actionlistener> tag to <p:commandbutton> multiple times instead of defining the "actionListener" attribute. It works with command buttons, but not with menu items. Any thoughts?

I'd like this to be available for PrimeFaces Mobile and Regular.
PrimeFaces 6.0 / JSF 2.2 Mojarra / GlassFish 4.1.1 / NetBeans 8.2

User avatar
sudheer
PrimeFaces Core Developer
Posts: 4345
Joined: 16 Oct 2011, 19:19
Location: Singapore

08 May 2015, 13:20

Why you need multiple actionListeners?Any specific reason?
Author,Speaker
https://twitter.com/sudheerjonna
Github: https://github.com/sudheerj
Website http://sudheerjonna.com/

___________________
Sudheer Jonna

kwrobel
Posts: 12
Joined: 17 Jan 2013, 23:02
Location: Chicagoland, Illinois, U.S.A.
Contact:

08 May 2015, 15:53

Yes. In my app I need two action listeners: one listener that processes something specific for an entity (an entity controller) inside a ViewScoped managed bean, and a second listener that keeps track of visited pm:pages inside a SessionScoped bean. Works perfectly with a command button, but apparently not with a menu item.
PrimeFaces 6.0 / JSF 2.2 Mojarra / GlassFish 4.1.1 / NetBeans 8.2

kwrobel
Posts: 12
Joined: 17 Jan 2013, 23:02
Location: Chicagoland, Illinois, U.S.A.
Contact:

08 May 2015, 17:46

Btw: here's a documented example of using multiple action listeners over on http://archive.oreilly.com/pub/a/onjava ... tml?page=3. Scroll down to "Using an Action Listener Method or Instance". The article states that "you can attach as many ActionListener instances as you want to the component".

I'd imagine this to be true in PrimeFaces as well, but at least MenuItem does not function properly when using more than one action listener.
PrimeFaces 6.0 / JSF 2.2 Mojarra / GlassFish 4.1.1 / NetBeans 8.2

kwrobel
Posts: 12
Joined: 17 Jan 2013, 23:02
Location: Chicagoland, Illinois, U.S.A.
Contact:

11 May 2015, 17:54

Ok. I found out that p:menuitem does in fact work properly with multiple action listeners. I made the mistake of not supplying the proper signature to the binding attribute. Here is an example of what it should look like:

Code: Select all

<p:menuitem value="Generate UUID" update="@form">
    <f:actionListener binding="#{indexBean.menuClickListener(event)}"/>
    <f:actionListener binding="#{sessionBean.loggerListener(event)}"/>
</p:menuitem>
Notice how there is

Code: Select all

(event)
next to the listener method name. I originally didn't have that. Now it works.
PrimeFaces 6.0 / JSF 2.2 Mojarra / GlassFish 4.1.1 / NetBeans 8.2

kwrobel
Posts: 12
Joined: 17 Jan 2013, 23:02
Location: Chicagoland, Illinois, U.S.A.
Contact:

11 May 2015, 18:31

Ok, so now I have a different issue: the ActionEvent inside the listener is null! However, if I use the actionListener attribute and omit the (event) after the listener name, the event is not null.

Can anybody please assist?
PrimeFaces 6.0 / JSF 2.2 Mojarra / GlassFish 4.1.1 / NetBeans 8.2

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

12 May 2015, 14:13

What you did by adding the (event) parameter to the actionListener in the EL, is make it match the method signature of the method in the actionListener on the java side (what is that methodSignature btw?). It does not mean that 'event' has an actual value (try what happens if you rename event in the call to 'bla'? Does it exhibit the same behaviour or does it throw errors?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 26 guests