Search code examples
jsfprimefacesreload

Reload page from backing bean


In my code, I'm expanding treeNode in left frame that is selected through navigation links present in right frame. It works but everytime I click the link on right frame, I have to refresh the right frame manually. I tried to reload the page from backing bean using javascript code but it's not working. Can anyone please help me to figure out why it's not getting executed.

Thanks in advance for helping me out.

Below is the code I'm using.

public void expandTreeView( TreeNode selectedNode )
{
    if ( selectedNode != null )
    {
        selectedNode.getParent().setExpanded( true );

    }
    RequestContext rc = RequestContext.getCurrentInstance();
    rc.execute("window.location.reload(true)");

}

Solution

  • You need to combine a JS Function with remoteCommand, it will look like this :

    myHTML.xhtml

    <p:commandLink id="commandLink" onclick="myFunction(nodeSelected)"  >
    ...
    </p:commandLink>   
    

    Also add a JS function

    <script type="text/javascript">
       function myFunction(x) {                                
           ...                                
        }    
    </script>
    

    and finally combine it with a p:remoteCommand it allows you to call a managedBean method from your JS function

    You can see Primefaces remoteCommand Example or simply look to this SO post Invoking a p:remoteCommand via a JavaScript function passing a message local to that function to another function through the “oncomplete” handler

    Hope that helped you.