Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从javascript调用支持bean方法_Java_Javascript_Spring_Jsf_Primefaces - Fatal编程技术网

无法从javascript调用支持bean方法

无法从javascript调用支持bean方法,java,javascript,spring,jsf,primefaces,Java,Javascript,Spring,Jsf,Primefaces,我尝试用bean方法编写javascript代码。我成功地开始了这项申请 当我运行我的应用程序时,js函数调用bean方法并返回值,但是当我单击按钮时,它再也不会调用。我的意思是,它只在应用程序启动时调用bean方法。 我的按钮是: <p:commandLink oncomplete="myRemote();" title="my button" /> 我的豆豆是: @Controller @Scope("view") public class MyB

我尝试用bean方法编写javascript代码。我成功地开始了这项申请

当我运行我的应用程序时,js函数调用bean方法并返回值,但是当我单击按钮时,它再也不会调用。我的意思是,它只在应用程序启动时调用bean方法。 我的按钮是:

<p:commandLink oncomplete="myRemote();" title="my button"
               />
我的豆豆是:

@Controller
@Scope("view")
public class MyBackingBean implements Serializable {


public String actionMyValues() {

    String js="";
    /*getting some values from database and adding it to javascript variables.*/
    js += "alert('alert')";//
    return js;
}
}

如果要从支持bean调用JS,可以使用org.primefaces.context.RequestContext类:

编辑

正如注释中所建议的,您也可以使用p:remoteCommand

远程命令将替换您的自定义JS函数:

<p:commandButton value="My button" type="button" onclick="myRemote()" id="btnRemote" />  

<p:remoteCommand name="myRemote" actionListener="#{myBackingBean.actionMyValues()}"/>   

然后,如果您需要从bean调用JS,您仍然需要使用RealestFrase.< /P>我不清楚这个问题,但是为什么不考虑使用P:RetryEcMoMand?我的设计就像在客户机中编写JavaScript代码作为字符串,并将其传递给客户端。当应用程序运行时,这只工作一次。但是,如果我尝试通过单击按钮来实现,javascript函数的内容不会改变。Js函数不调用bean方法,因此它不会获取字符串的新内容。据我所知,问题是另一种方式,从Js调用服务器端方法。事实上,在建议的代码中,从服务器端执行Js的部分是错误的。我制作并编辑了,一个更好的解决方案是用一个p:remoteCommand替换整个程序,正如Hatem所建议的那样这是问题的另一个方面\

String js="";
/*getting some values from database and adding it to javascript variables.*/
js += "alert('alert')";
RequestContext.getCurrentInstance().execute("js");
<p:commandButton value="My button" type="button" onclick="myRemote()" id="btnRemote" />  

<p:remoteCommand name="myRemote" actionListener="#{myBackingBean.actionMyValues()}"/>