我有一个菜单,看起来像:
<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
没有被调用,单击菜单项时也不会发生任何事情。
所以,当我调用错误的方法时,这一切看起来都是一样的。
从更改bean方法
public void setCurrentTab(int currentTab) {
this.currentTab = currentTab;
}
至
public void setCurrentTab(Long currentTab) {
this.currentTab = currentTab.intValue();
}
解决了这个问题。
经过几个小时的挣扎,我终于弄明白为什么这不起作用了。
默认情况下,actionListener
需要一个名为changeTab(Long currentTab)
的方法,但我的bean中有一个changeTab(int currentTab)
。所以,我基本上是在尝试调用一个不存在的方法。框架没有抛出任何错误,可能是因为p:menuitem
默认使用ajax
。只有当我在菜单项上显式设置ajax="false"
时,我才开始得到错误,即"method doesn't exist"
。
我已经两次陷入这个陷阱,浪费了很多时间去弄清楚。所以把这个放在这里,这样它就能帮助别人。