Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call multiple methods from <p:ajax event=select listner=method1, metho2>?

Can I call multiple methods from the ajax event select in listener?

<p:tree value="#{ddTreeBean.root}" var="node" dynamic="true"
                selectionMode="single" selection="#{ddTreeBean.selectedNode}">

<p:ajax event="select" listener="#{data2.refresh}"
                    update=":pchartId,:panelId">
                    </p:ajax>
        <p:treeNode type="node" expandedIcon="folder-open"
                    collapsedIcon="folder-collapsed">
                    <h:outputText value="#{node.name}" />
                </p:treeNode>

                <p:treeNode type="leaf" icon="document-node">
                    <h:outputText value="#{node.name}" />
                </p:treeNode>
            </p:tree>

on a select I need to bind my listener to two methods? Is that allowed?

I have a tree and when I make a selection, I need to update (trigger) two components (two other back beans). Does listener attribute take two parameters (two method names)? THanks.

Myclass1 class {
 method1();
 }



Myclass2 class {
 method2();

 }
like image 586
Qajussi Avatar asked Aug 25 '14 14:08

Qajussi


2 Answers

If you want to call a method of one ManagedBean from another, you have to Inject the other ManagedBean.

@ManagedBean
public class MyBean1{

   public void methodAbc(){
     ...
   }
}

Inject in to

@ManagedBean
public class MyBean2{

   @ManagedProperty(value = "#{myBean1}")
   private MyBean1 mybean1;

   //SETTER GETTER for mybean1

   public void myAction(){
     mybean1.methodAbc();
   }
}

Compatible ManagedBean Injection scoped are given in following table(courtesy of Core Java Server Faces Book): enter image description here

OR You can dynalically resolve EL expression in your Action method itself as follows.

public void myAction(){
   FacesContext fctx = FacesContext.getCurrentInstance();
   MyBean1 mybean1 = fctx.getApplication().evaluateExpressionGet(fctx , "#{myBean1}", MyBean1.class);
   mybean1.methodAbc();
}

Since you are using Primefaces there is one more way to do this, using p:remoteCommand :

<p:ajax event="select" listener="#{data2.refresh}"
        update=":pchartId,:panelId" 
        oncomplete="callRemote2()"/>

<p:remoteCommand name="callRemote" partialSubmit="true" process="@this" 
                 action="#{yourmanagedbean.method2}" />
like image 112
Kishor Prakash Avatar answered Sep 28 '22 11:09

Kishor Prakash


No, it doesn't. You can have a single method where you call the two or more methods you need to execute:

<p:ajax event="select" listener="#{someMB.multipleMethods}" update=":pchartId,:panelId" />

And in Java side

@ManagedBean
@ViewScoped
public class SomeMB {
    public void method1() { /* */ }
    public void method2() { /* */ }
    public void multipleMethods() { 
        method1();
        method2();
    }
}

If you need to use several managed beans, an option is to inject one into the other:

@ManagedBean
@SessionScoped
public class AnotherMB {
    public void method2() { /* */ }
}

@ManagedBean
@ViewScoped
public class SomeMB {

    @ManagedProperty("#{anotherMB}")
    AnotherMB anotherMB;

    //provide a setter
    public void setAnotherMB(AnotherMB anotherMB) {
        this.anotherMB = anotherMB;
    }

    public void method1() { /* */ }
    public void multipleMethods() { 
        method1();
        anotherMB.method2();
    }
}
like image 29
Luiggi Mendoza Avatar answered Sep 28 '22 11:09

Luiggi Mendoza