【问题标题】:error in calling a valueChangeListener method on "listener" attribute on <f:ajax> for event="valueChange"为 event="valueChange" 在 <f:ajax> 的“listener”属性上调用 valueChangeListener 方法时出错
【发布时间】:2013-02-05 08:43:45
【问题描述】:

我正在使用以下规格:

JSF 2.0、带有 JDK 1.6 的 Netbeans 7.1.2 和 Glassfish 3.1.2

下面是我的 index.xhtml 代码:

<h:form>
    <h:panelGrid columns="2">
        <h:outputText value="Select a country:" />
        <h:selectOneMenu id="countryBox" value="#{ab.selCountry}">
            <f:selectItem itemLabel="India" itemValue="India" />
            <f:selectItem itemLabel="United States of America" itemValue="USA" />
            <f:ajax execute="countryBox" render="stateBox" event="valueChange" listener="#{ab.result}"/>
        </h:selectOneMenu> 
        <h:outputText value="Select a state:" />
        <h:selectOneMenu value="#{ab.country}" id="stateBox">
            <f:selectItems value="#{ab.state}" />
        </h:selectOneMenu>
    </h:panelGrid>
</h:form>

这是我的 AJAXBean.java 代码:

@ManagedBean (name="ab")
@RequestScoped
public class AJAXBean {

    /**
     * Creates a new instance of AJAXBean
     */

    private List<SelectItem> country;
    private List<SelectItem> state;
    private String selCountry;

    // getters and setters of properties

    public AJAXBean() 
    {
        country = new ArrayList<SelectItem>();
        state = new ArrayList<SelectItem>();
    }

    public void result(ValueChangeEvent vce) // valueChangeListener for ajax call. Is this valid?
    {
        System.out.println("Inside valueChangeListener");

        String value = (String) vce.getNewValue();
        state.clear();

        if (value.equals("India"))
        {
            state.add(new SelectItem("Maharashtra"));
            state.add(new SelectItem("Madhya Pradesh"));
            state.add(new SelectItem("Andhra Pradesh"));
            state.add(new SelectItem("Uttar Pradesh"));
            state.add(new SelectItem("Rajasthan"));

        }
        else if (value.equals("USA"))
        {
            state.add(new SelectItem("Missouri"));
            state.add(new SelectItem("Kentucky"));
            state.add(new SelectItem("North Carolina"));
            state.add(new SelectItem("South Carolina"));
            state.add(new SelectItem("Colorado"));

        }
    }
}

基本上我在这里要做的是:

在 event="valueChange" 和 listener="#{ab.result}" 上触发 AJAX 调用。此处的结果方法修改了(SelectItems 的)“状态”列表的值。

我的值更改侦听器方法的语法是否正确?因为当我尝试执行此操作时,它会显示一个警告框,说明 MethodNotFoundException + 方法 #{ab.result(AJAXBehaviorEvent)} 未找到,即它正在尝试搜索捕获 AJAXBehaviorEvent 对象的“结果”方法。所以,我的问题是,值更改侦听器方法的签名与 f:ajax 调用一起使用时是否有任何不同?

PS:它尝试使用#{ab.result()}。在这种情况下,它只是说 MethodNotFoundException。 如果使用 #{ab.result(vce)} 而不是 #{ab.result()} / #{ab.result},它会给我一个 NullPointerException。

如果我将相同的函数放在 h:selectOneMenu 的 valueChangeListener 属性中,它会起作用。我的意思是,显然它会起作用。但这并不能解决我的目的。我的目的是对 valueChange 事件进行简单的 ajax 调用,它更新 bean 中的列表并重新呈现(第二个)链接的 h:selectOneMenu

我的代码有什么问题?

【问题讨论】:

  • 采用 AJAXBehaviourEvent,而 valueChangeListener 采用 ValueChangeEvent。你只是放错地方了。
  • @hemanth,你是对的,你为什么不发布一个详细的答案
  • @kolossus 对不起,我没有时间详细回答,只是想给 OP 一个提示。

标签: jsf-2


【解决方案1】:

听听你遇到的异常。它说它找不到带有AjaxBehaviorEvent 参数的方法。将ValueChangeEvent 参数更改为AjaxBehaviorEvent 参数。

public void result(AjaxBehaviorEvent event) {
    // ...
}

直接访问selCountry,而不是ValueChangeEvent#getNewValue()

顺便说一下,event="valueChange" 已经是默认设置了。只需摆脱它以减少代码噪音。

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-03
    • 2015-11-12
    • 2015-07-19
    • 2012-08-06
    • 2011-08-14
    • 2023-02-19
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多