f:没有调用ajax监听器

时间:2012-11-27 09:50:57

标签: ajax jsf-2 listener

无论我做什么,我都无法理解f:ajax标签的listener属性。该方法不会被调用。我没有收到任何错误消息。这是一个例子:

test.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>
            Example
        </title>
    </h:head>
    <h:body>
        <h:form>
            <h:selectOneMenu value="#{testBean.value}">
                <f:selectItem itemLabel="1" itemValue="1" />
                <f:selectItem itemLabel="2" itemValue="2" />                    
                <f:ajax render="messages" listener="#{testBean.processAjaxBehaviour}" onevent="test" />
            </h:selectOneMenu>
            <h:messages id="messages" />
        </h:form>
        <script type="text/javascript">
            function test(event)
            {
                alert(event.status);
            }
        </script>
    </h:body>
</html>

TestBean.java

package test;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.AbortProcessingException;

@ManagedBean
@ViewScoped
public class TestBean implements Serializable
{
    int value = 2;

    public int getValue()
    {
        return value;
    }

    public void setValue(int value)
    {
        value = value;
    }

    @PostConstruct
    public void init()
    {
        System.out.println("Done constructing testBean.");
    }

    public void processAjaxBehaviour(AjaxBehaviorEvent event) throws AbortProcessingException
    {
        System.out.println("Processing AJAX behaviour.");
    }
}

按预期方式调用函数test三次,我获得状态“成功”。但是,不会调用侦听器processAjaxBehaviour

调用init方法。 option呈现的第二个f:selectItem元素会按预期选择。

0 个答案:

没有答案