Himanshu Arora
Himanshu Arora

Reputation: 2568

p:menuitem not invoking action listener

I have a menu which looks like:

<h:form>
    <p:menu>
        <p:menuitem value="One" actionListener="#{tabsVO.changeTab(1)}" update="tabView"/>
        <p:menuitem value="Two" actionListener="#{tabsVO.changeTab(2)}" update="tabView"/>                              
        <p:menuitem value="Three" actionListener="#{tabsVO.changeTab(3)}" update="tabView"/>
    </p:menu>
</h:form>

Corresponding Bean:

@ManagedBean
@ViewScoped
public class TabsVO{    

    private int currentTab;

    @PostConstruct
    public void init() {
        currentTab = 0;
    }

    public void changeTab(int tabIndex){
        this.currentTab = tabIndex;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public void setCurrentTab(int currentTab) {
       this.currentTab = currentTab;
    }
}

Everything seems fine but the action listener is not invoked and nothing happens on clicking the menu items.

Upvotes: 0

Views: 558

Answers (1)

Himanshu Arora
Himanshu Arora

Reputation: 2568

So, it looks like all this while I was calling the wrong method.

Changing the bean method from

 public void setCurrentTab(int currentTab) {
     this.currentTab = currentTab;
 } 

to

 public void setCurrentTab(Long currentTab) {
     this.currentTab = currentTab.intValue();
 } 

solved the issue.

After hours of struggling I could figure out the reason why this was not working.

By default the actionListener was expecting a method called changeTab(Long currentTab) but what I had in my bean was changeTab(int currentTab). So, I was basically trying to invoke a method that didn't exist. And no error was being thrown by the framework probably because p:menuitem uses ajax by default.Only when I explicitly set ajax="false" on the menuitems, I started getting the error which said "method doesn't exist".

I have fallen into this trap twice now and wasted a lot of time figuring it out. So putting this here, so that it helps someone.

Upvotes: 1

Related Questions