2017-08-29 17 views
0

我看起來像一個菜單:號碼:菜單項不調用動作監聽

<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> 

相應的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; 
    } 
} 

一切似乎都不錯,但不調用action listener並沒有任何反應上點擊菜單項。

回答

0

因此,它看起來像所有這些,而我正在調用錯誤的方法。

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

更改bean的方法

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

解決的問題。

經過數小時的掙扎後,我可以找出爲什麼這不起作用。

默認情況下,actionListener期待一種叫做changeTab(Long currentTab)的方法,但是我在我的bean中擁有的方法是changeTab(int currentTab)。所以,我基本上試圖調用一個不存在的方法。 並沒有錯誤被框架拋出可能是因爲​​默認使用ajax。只有當我明確設置ajax="false"在菜單項上,我開始得到錯誤,說"method doesn't exist"

我現在陷入了這個陷阱兩次,浪費了很多時間搞清楚了。所以把它放在這裏,以便它可以幫助別人。