Not able to add new ajax behavior to primefaces datatable dynamically when it already have listeners added in xhtml

376 views Asked by At

I'm using primefaces-6.0, mojarra-2.2.12 and wildfly-8.2. My goal is to add columnresize listener to datatable dynamically using prerenderview listener. This solution is working fine when there no ajax listener is attached previously. But if the datatable have any other ajax listeners(say rowselect) and the resize listener is added using prerenderview listener , i'm getting null pointer exception at the JSF implemention when i try to update the rendered datatable.

xhtml code:

<f:event type="preRenderView"  listener="#{datatableTestBean.onPreRenderViewEvent()}"/>

 <p:panelGrid>
            <p:row>
                <p:column>
                    <p:commandButton  value="refersh datatable" actionListener="#{datatableTestBean.refershAll()}" update="-frm3"/>
                </p:column>
            </p:row>
            <p:row>
                <p:column>
                    <p:dataTable id="scrolltestdt" value="#{datatableTestBean.dtValues}" var="row"
                                 resizableColumns="true"  liveResize="false" resizeMode="expand"
                                 scrollable="true" scrollHeight="400" selectionMode="single" rowKey="#{row}">
                        <p:ajax event="rowSelect" listener="#{datatableTestBean.onRowSelected()}"/>

                        <p:column filterBy="#{row[0]}" headerText="COlumn1" >
                            <h:outputText value="#{row[0]}"/>
                        </p:column>
                        <p:column filterBy="#{row[1]}" headerText="COlumn2"> 
                            <h:outputText value="#{row[1]}" />
                        </p:column>


                    </p:dataTable>
                </p:column>
            </p:row>

CDI bean code:

    int tmp = 0;

public void onPreRenderViewEvent() {
    if (tmp == 0) {
        System.out.println("onPreRenderViewEvent");
        DataTable table = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("frm3-scrolltestdt");
        addColumnResizeListenerToDatatable(table);
        tmp = 1;
    }
}

private void addColumnResizeListenerToDatatable(DataTable table) {
    FacesContext fc = FacesContext.getCurrentInstance();

    ExpressionFactory ef = fc.getApplication().getExpressionFactory();
    MethodExpression me = ef.createMethodExpression(fc.getELContext(), "#{datatableTestBean.onResize}", Object.class, new Class[]{ColumnResizeEvent.class});

    AjaxBehavior columnResizeEvent = (AjaxBehavior) fc.getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);

    columnResizeEvent.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(me, me));

    table.addClientBehavior("colResize", columnResizeEvent);

}

This error is coming when i update the form using command button.

log:

[javax.enterprise.resource.webcontainer.jsf.context] (default task-54) java.lang.NullPointerException
at javax.faces.component.UIComponentBase.restoreBehaviors(UIComponentBase.java:2228)
at javax.faces.component.UIComponentBase.restoreBehaviorsState(UIComponentBase.java:2203)
at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1623)
at javax.faces.component.UIData.restoreState(UIData.java:1750)
at org.primefaces.component.api.UIData.restoreState(UIData.java:1311)
at com.sun.faces.application.view.FaceletPartialStateManagementStrategy$2.visit(FaceletPartialStateManagementStrategy.java:379)
at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151)
at org.primefaces.component.api.UIData.visitTree(UIData.java:827)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIForm.visitTree(UIForm.java:371)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at com.sun.faces.application.view.FaceletPartialStateManagementStrategy.restoreView(FaceletPartialStateManagementStrategy.java:366)
at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:138)
at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:123)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:591)
at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:151)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at org.omnifaces.viewhandler.OmniViewHandler.restoreView(OmniViewHandler.java:100)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:123)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130)
at io.undertow.websockets.jsr.JsrWebSocketFilter.doFilter(JsrWebSocketFilter.java:129)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76)
1

There are 1 answers

0
Ilya On

I had same problem when trying to add ajaxBehavior in onPreRenderViewEvent listener.

Try to do it in listener of

<f:event type="postAddToView" listner="#{listner}">

It works for me.