Using backing bean value in javascript

前端 未结 2 956
半阙折子戏
半阙折子戏 2021-01-26 06:20
   
                              
                        &l         


        
相关标签:
2条回答
  • 2021-01-26 06:56

    That's just because alert(#{test.i}); is evaluated when the commandButton is rendered. You can see the changed value by telling JSF to render the script again:

    <h:commandButton value="click me" action="#{testClass.testfxn()}">
        <f:ajax render="out" />
    </h:commandButton>
    <h:panelGroup id="out">
        <h:outputScript>
             alert(#{testClass.i});
        </h:outputScript>
    </h:panelGroup>
    
    0 讨论(0)
  • 2021-01-26 07:12

    Unlike Richfaces where you can directly call javascript function, primefaces you need to have javascript function as:

    function_name(xhr, status, args)

    Using listed CommandButton as Example:

     <p:commandButton value="GO" oncomplete="alert(#{test.i})"  actionListener="#{test.testfxn()}" update="@this"/>
    

    In function test.testfxn() we have:

    public void testfxn(){    
    RequestContext reqCtx = RequestContext.getCurrentInstance();
            i = i++;
            reqCtx.addCallbackParam("i", i);
    }
    

    Here,In function call to backing bean from actionlistener the variables are added to RequestContext.

    Now In javascript function:

    function draw(xhr, status, args) {
        console.log("We are into draw function");
        jsonString = JSON.parse(args.i); //json variable is taken out from args variable.
        console.log("The value of i "+ i);
    }
    
    0 讨论(0)
提交回复
热议问题