0

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

测试.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被调用了 3 次,我得到了“成功”状态。processAjaxBehaviour但是,不会调用侦听器。

init方法被调用。option由 渲染的第二个元素f:selectItem按预期选择。

4

0 回答 0