开发者

JSF2.1 different behavior ajax listener used in page with facelets or without facelets

开发者 https://www.devze.com 2023-03-15 22:48 出处:网络
If a commandButton with ajax support used in een .xhtml without facelets templates, the ajax listener has to be an attribute of the commandButton-tag. If used in conjunction with a template, the liste

If a commandButton with ajax support used in een .xhtml without facelets templates, the ajax listener has to be an attribute of the commandButton-tag. If used in conjunction with a template, the listener has to be an attribute of the ajax-tag.

I needed some time to figure this out so if this is correct and a normal behavior, be warned.

The 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;
    }
}

the first 2 buttons work in both pages (with and without using facelets). The other buttons don't see the buttons value attribute.

the .xhtml using 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:commandButt开发者_如何学Con>
    </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>

the .xhtml without facelets

<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>

the template file

<?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

精彩评论

暂无评论...
验证码 换一张
取 消