JSF2.1不同的行为ajax监听器在页面中使用facelets或没有facelets

时间:2011-06-29 08:09:47

标签: ajax jsf facelets

如果在没有facelets模板的情况下在een .xhtml中使用了带有ajax支持的commandButton,则ajax侦听器必须是commandButton-tag的属性。如果与模板一起使用,则侦听器必须是ajax-tag的属性。我需要一些时间来弄清楚这一点,所以如果这是正确的和正常的行为,请注意。

viewscoped bean

@ManagedBean
@ViewScoped
public class Test implements Serializable {
    private static final long serialVersionUID = 123456L;
    private static int i = 0;
    private int counter;
    private String test = "test ";

    @PostConstruct
    public void test() {
        System.out.println(".......... PostConstruct");
        i++;
    }

    public String getTest() {
        return test + i;
    }

    public String action() {
        counter++;
        System.out.println(".......... action() " + counter);
        return "";
    }

    public void listener() {
        counter++;
        System.out.println(".......... listener() " + counter);
    }

    public void ajaxListener(AjaxBehaviorEvent actionEvent) {
        System.out.println(".......... ajaxListener() - " + 
                           "AjaxBehaviorEvent: " + actionEvent);
    }

    public void ajax() {
        System.out.println(".......... ajax() - not using Facelets " + 
                           counter);
    }

    public void ajaxList2(ActionEvent actionEvent) {
        System.out.println(".......... ajaxList2() - ActionEvent" + 
                           actionEvent);
    }

    public int getCounter() {
        return counter;
    }
}

前两个按钮在两个页面中都有效(使用和不使用facelets)。其他按钮看不到按钮值属性。

.xhtml使用facelets

<ui:composition  xmlns="http://www.w3.org/1999/xhtml"
             xmlns:h="http://java.sun.com/jsf/html"
             xmlns:f="http://java.sun.com/jsf/core"
             xmlns:ui="http://java.sun.com/jsf/facelets"
             xmlns:p="http://primefaces.prime.com.tr/ui"
             template="/WEB-INF/templates/test.xhtml">
<ui:define name="ui_content">

    #{test.test}

    <h:form id="frm">
        <h:commandButton value="testAction" action="#{test.action}"/>
        <h:commandButton value="testListener" actionListener="#{test.listener}"/>
        <h:commandButton value="Ajax - works ONLY using Facelets" immediate="true"  >
            <f:ajax event="click" execute="@form" render=":grp2" listener="#{test.ajaxListener}" />
        </h:commandButton>
        <h:commandButton value="testAjax - works ONLY without using Facelets" immediate="true" action="#{test.ajax}" >
            <f:ajax event="click" execute="@form" render=":grp2"/>
        </h:commandButton>
        <h:commandButton value="testAjax - actionListener works ONLY without using Facelets"
                         immediate="true" actionListener="#{test.ajaxList2}" >
            <f:ajax event="click" execute="@form" render=":grp2"/>
        </h:commandButton>
    </h:form>
    <h:panelGroup id="grp1" rendered="#{test.counter > 2}">
        1st group: #{test.counter}
    </h:panelGroup><br />
    <h:panelGroup id="grp2" rendered="#{test.counter > 7}">
        2de group: #{test.counter}
    </h:panelGroup>

没有facelets的.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
             xmlns:h="http://java.sun.com/jsf/html"
             xmlns:f="http://java.sun.com/jsf/core"
             xmlns:p="http://primefaces.prime.com.tr/ui">

    #{test.test}

    <h:form id="frm">
        <h:commandButton value="testAction" action="#{test.action}"/>
        <h:commandButton value="testListener" actionListener="#{test.listener}"/>
        <h:commandButton value="Ajax - works ONLY using Facelets" immediate="true"  >
            <f:ajax event="click" execute="@form" render=":grp2" listener="#{test.ajaxListener}" />
        </h:commandButton>
        <h:commandButton value="testAjax - action works ONLY without using Facelets" immediate="true" action="#{test.ajax}" >
            <f:ajax event="click" execute="@form" render=":grp2"/>
        </h:commandButton>
        <h:commandButton value="testAjax - actionListener works ONLY without using Facelets" immediate="true" actionListener="#{test.ajaxList2}" >
            <f:ajax event="click" execute="@form" render=":grp2"/>
        </h:commandButton>
    </h:form>
    <h:panelGroup id="grp1" rendered="#{test.counter > 2}">
        1st group: #{test.counter}
    </h:panelGroup><br />
    <h:panelGroup id="grp2" rendered="#{test.counter > 7}">
        2nd group: #{test.counter}
    </h:panelGroup>
</html>

模板文件

<?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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">              
    <h:head>      
        <title>test</title>
    </h:head>
    <h:body>
        <f:view>
            <div id="page">
                <ui:insert name="ui_header">header</ui:insert>
                <ui:insert name="ui_subnav"/>
                <div id="content">
                    <ui:insert name="ui_content"/>
                </div>
                <ui:insert name="ui_footer" />
            </div>
        </f:view>      
    </h:body>
</html>

0 个答案:

没有答案