本文介绍了我可以从< p:ajax event = select listner = method1,metho2>调用多个方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从侦听器中的ajax事件中调用多个方法吗?

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?

我有一棵树,当我做出选择时,我需要更新(触发)两个组件(另外两个back bean).侦听器属性是否带有两个参数(两个方法名称)?谢谢.

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();

 }

推荐答案

如果要从另一个ManagedBean调用一个方法,则必须注入另一个ManagedBean.

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(){
     ...
   }
}

插入

@ManagedBean
public class MyBean2{

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

   //SETTER GETTER for mybean1

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

下表中给出了

兼容的ManagedBean Injection范围(由Core Java Server Faces Book提供):

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

或者您可以按以下方式在Action方法本身中动态解析EL表达式.

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();
}

由于您使用的是Primefaces,因此还有另一种方法,可以使用p:remoteCommand:

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}" />

这篇关于我可以从&lt; p:ajax event = select listner = method1,metho2&gt;调用多个方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:31