[英]JSF Not a Valid Method Expression for Richfaces' a4j:ajax listener
[英]a4j:ajax listener exception MethodNotFoundException
我開始研究RichFaces 4.2.2,並在一個簡單示例中遇到了問題,我有一個xml:
<ui:define name="content">
<h:form>
<rich:panel style="width: 50%">
<h:panelGrid columns="2">
<h:outputText value="Name:"/>
<h:inputText id="inp" value="#{echoBean.name}">
<a4j:ajax event="keyup" render="echo count" listener="#{echoBean.countListener}"/>
</h:inputText>
<h:outputText value="Echo:"/>
<h:outputText id="echo" value="#{echoBean.name}"/>
<h:outputText value="Count:"/>
<h:outputText id="count" value="#{echoBean.count}"/>
</h:panelGrid>
<a4j:commandButton value="Submit" actionListener="#{echoBean.countListener}" render="echo, count"/>
</rich:panel>
</h:form>
</ui:define>
和一個簡單的bean:
@Component("echoBean")
@Scope(value = "session")
public class EchoBean {
private String name;
private Integer count = 0;
//getter setter methods here
public void countListener(ActionEvent event) {
count++;
}
}
當我嘗試在inputText中打印時,出現異常:
Caused by: javax.el.MethodNotFoundException: /home.xhtml @35,112 listener="#{echoBean.countListener}": Method not found: com.example.training.bean.EchoBean@d523fa.countListener()
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at org.ajax4jsf.component.behavior.MethodExpressionAjaxBehaviorListener.processAjaxBehavior(MethodExpressionAjaxBehaviorListener.java:71)
at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:113)
at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:98)
at org.ajax4jsf.component.behavior.AjaxBehavior.broadcast(AjaxBehavior.java:348)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:763)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
... 19 more
但為什么? 使用按鈕,這個相同的偵聽器可以正常工作,並且在a4j:ajax中的“ listener”參數的文檔中說:
該表達式必須計算為帶ActionEvent參數且返回類型為void的公共方法,或者為不帶參數返回值類型為void的公共方法。
為什么使用不帶ActionEvent
參數的countListener()
? 我不明白
為了使RF4可以使用listener
屬性,您的listener方法應采用AjaxBehaviorEvent
類型的參數,而不是ActionEvent
類型的參數。 從錯誤消息中可以看到的另一種替代方法是定義一個標准的Java方法,該方法不帶參數,並且返回類型為void
public void countListener();
為什么使用不帶ActionEvent參數的countListener()? 我不明白
那是API的合同,您必須遵守才能使用它。
使用以下簽名無效的bean函數作為返回類型ActionEvent對象作為參數bean函數的示例如下
public void countListener(ActionEvent event) {}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.