Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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
Jsf 如何将参数(从javascript)传递到remoteCommand(然后将其发送到支持bean)?_Jsf_Primefaces - Fatal编程技术网

Jsf 如何将参数(从javascript)传递到remoteCommand(然后将其发送到支持bean)?

Jsf 如何将参数(从javascript)传递到remoteCommand(然后将其发送到支持bean)?,jsf,primefaces,Jsf,Primefaces,要将参数从JS传递到p:remoteCommand(由primefaces提供),可以使用以下方法: remoteCommandFunctionName({name1:'value1', name2:'value2'}); 在此之后,您如何在remoteCommand中接收这组参数,以便将其发送到支持bean?如果您定义了如下远程命令: <p:remoteCommand name="remoteCommandFunctionName" actionLi

要将参数从JS传递到
p:remoteCommand
(由primefaces提供),可以使用以下方法:

remoteCommandFunctionName({name1:'value1', name2:'value2'});

在此之后,您如何在
remoteCommand
中接收这组参数,以便将其发送到支持bean?

如果您定义了如下远程命令:

<p:remoteCommand name="remoteCommandFunctionName" 
                 actionListener="#{myBean.exec}"/>
您不需要指定传递给Javascript方法调用到remoteCommand的参数。我认为在支持bean中毕竟需要这些参数。您可以使用请求参数映射来获取支持bean方法中JavaScript调用中传递的参数值:

public void exec() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map map = context.getExternalContext().getRequestParameterMap();
    String name1 = (String) map.get("name1");
    String name2 = (String) map.get("name2");
}

厚颜无耻地插入我的答案,因为在PrimeFace 3.3中解决这个问题需要花费我几个小时。解决方案是将参数作为{name:,value:}数组传递

正如Neyko的回答一样,调用应更改为:

remoteCommandFunctionName([{name: 'name1', value: 'value1'}, {name: 'name2', value: 'value2'}]);

我用primefaces 4.0尝试了以前的解决方案,但它们对我不起作用

因此,作为一种解决方法,我必须放置一个
,并将该值设置为ManagedBean的属性, 在调用
之前,我设置了这个h:inputHidden的值(使用jQuery)并调用p:remoteCommand(确保远程命令正在处理h:inputHidden)

FormBean.java

form.xhtml

<p:remoteCommand name="remoteCommandFunctionName" actionListener="#{myBean.exec}"/>

编辑

这也非常有效

remoteAction([{name: 'name1', value: 'value1'}, {name: 'name2', value: 'value2'}]);

希望这有助于…

来自instcode的解决方案在primefaces 4.0中工作

xhtml

<p:remoteCommand name="remoteCommandFunctionName" actionListener="#{myBean.exec}"/>
JavaScript

remoteCommandFunctionName([{name: 'name1', value: 'value1'}, {name: 'name2', value: 'value2'}]);

那么,如何通过javascript传递这些请求参数呢?实际上,我还没有找到足够的信息来说明如何将javascript传递的参数传递到支持bean中。我在这里提供的信息与我使用PrimeFaces remoteCommand的经验有关。可能有更好的实现方法。感谢将测试此方法是否适用于MESIDE注意:getRequestParameterMap()是一个映射,如果将这些类型参数添加到映射中,则现在不需要在primfaces 3.4.2中强制转换.works。从3.2迁移到3.4.2,调用停止。Neyko的答案也应该更新。是的,我很高兴我进一步阅读并得到了这个评论。@instcode有正确的答案,如果(像我一样)你第一次不理解它,让我们更明确一点:你必须以文本形式写“name:”和“value:”,因为它们是关键字。您不能将name:替换为自定义名称(例如name1…)。当然,冒号右边可以是js变量或“常量”。再次感谢!你解决了我的问题!这里的答案已经过时了,请看
remoteAction([{name: 'name1', value: 'value1'}, {name: 'name2', value: 'value2'}]);
<p:remoteCommand name="remoteCommandFunctionName" actionListener="#{myBean.exec}"/>
public void exec() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map map = context.getExternalContext().getRequestParameterMap();
    String name1 = (String) map.get("name1");
    String name2 = (String) map.get("name2");
}
remoteCommandFunctionName([{name: 'name1', value: 'value1'}, {name: 'name2', value: 'value2'}]);