从 JavaScript 向 p:remoteCommand 传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7221495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass parameter to p:remoteCommand from JavaScript
提问by Rajat Gupta
I want to pass value to remoteCommand
from javascript. If this is possible, how can I do that and how can I receive them in the backing bean?
我想remoteCommand
从 javascript传递值。如果这是可能的,我该怎么做以及如何在支持 bean 中接收它们?
采纳答案by Cagatay Civici
remoteCommandFunctionName({name1:'value1', name2:'value2'});
回答by BalusC
Yes, it is possible. How to do that depends on the PrimeFaces version. You can see it in PrimeFaces users guide.
对的,这是可能的。如何做到这一点取决于 PrimeFaces 版本。您可以在PrimeFaces 用户指南 中看到它。
PrimeFaces 3.3 or newer
PrimeFaces 3.3 或更新版本
Since PrimeFaces version 3.3 the syntax is as follows (copypasted from 3.3 users guide).
从 PrimeFaces 3.3 版开始,语法如下(从 3.3 用户指南复制粘贴)。
3.81 RemoteCommand
...
Passing Parameters
Remote command can send dynamic parameters in the following way;
increment([{name:'x', value:10}, {name:'y', value:20}]);
3.81 远程命令
...
传递参数
远程命令可以通过以下方式发送动态参数;
increment([{name:'x', value:10}, {name:'y', value:20}]);
This way offers the possibility to specify multiple values on a single parameter name. Parameters with single values like above are available the same way as the old way:
这种方式提供了在单个参数名称上指定多个值的可能性。具有上述单个值的参数的可用方式与旧方式相同:
@ManagedProperty("#{param.x}")
private int x;
@ManagedProperty("#{param.y}")
private int y;
(note: you can use Integer
in Mojarra, but not in MyFaces, this is further completely unrelated to <p:remoteCommand>
)
(注意:您可以Integer
在 Mojarra 中使用,但不能在 MyFaces 中使用,这与 完全无关<p:remoteCommand>
)
or in method of a broader scoped bean:
或使用范围更广的 bean 的方法:
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
int x = Integer.valueOf(params.get("x"));
int y = Integer.valueOf(params.get("y"));
If you need to specify a parameter with multiple values, then you could do it as follows:
如果您需要指定具有多个值的参数,则可以按如下方式进行:
functionName([{name:'foo', value:'one'}, {name:'foo', value:'two'}, {name:'foo', value:'three'}]);`
with in a request scoped bean:
在请求范围的 bean 中:
@ManagedProperty("#{paramValues.foo}")
private String[] foos;
or in method of a broader scoped bean:
或使用范围更广的 bean 的方法:
Map<String, String[]> paramValues = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap();
String[] foos = paramValues.get("foo");
PrimeFaces 3.2 or older
PrimeFaces 3.2 或更高版本
BeforePrimeFaces version 3.3 the syntax is as follows (copypasted from 3.2 users guide):
在PrimeFaces 3.3 版之前,语法如下(复制自 3.2 用户指南):
3.80 RemoteCommand
...
Passing Parameters
Remote command can send dynamic parameters in the following way;
increment({param1:'val1', param2:'val2'});
3.80 远程命令
...
传递参数
远程命令可以通过以下方式发送动态参数;
increment({param1:'val1', param2:'val2'});
It's available in the backing bean by usual means. E.g. in a request scoped bean:
它可以通过通常的方式在支持 bean 中使用。例如在请求范围的 bean 中:
@ManagedProperty("#{param.param1}")
private String param1;
@ManagedProperty("#{param.param2}")
private String param2;
or in method of a broader scoped bean:
或使用范围更广的 bean 的方法:
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String param1 = params.get("param1");
String param2 = params.get("param2");
This approach had however the disadvantage that you can't specify a single parameter with multiple values like as possible with normal HTML forms and HTTP request parameters (which is in real world used on e.g. multiple select dropdownlist and multiple select checkboxgroup).
然而,这种方法的缺点是您不能像使用普通 HTML 表单和 HTTP 请求参数一样指定具有多个值的单个参数(这在现实世界中用于例如多选下拉列表和多选复选框组)。
See also:
也可以看看:
回答by Joel Richard
Page:
页:
<p:remoteCommand name="command" action="#{bean.method}" />
JavaScript:
JavaScript:
command({param: 'value'});
Bean:
豆角,扁豆:
public void method() {
String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("param");
}
回答by Jonathan L
Combine @BalusC @Joel's post for a functional example
结合@BalusC @Joel 的帖子作为功能示例
<h:form>
<p:remoteCommand name="rcName" update="msgs" actionListener="#{remoteCommandView.beanMethod}" />
<p:growl id="msgs" showDetail="true" />
<p:commandButton type="button" onclick="rcName([{name:'model', value:'Buick Encore'}, {name:'year', value:2015}]);" value="Pass Parameters 1" /><br/>
<p:commandButton type="button" onclick="clicked();" value="Pass Parameters 2" />
</h:form>
<script type="text/javascript">
//<![CDATA[
function clicked(){
rcName([{name:'model', value: 'Chevy Volt'}, {name:'year', value:2016}]);
}
//]]>
</script>
@ManagedBean
public class RemoteCommandView {
public void beanMethod() {
// OR - retrieve values inside beanMethod
String model1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("model");
String year1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("year");
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Executed",
"Using RemoteCommand with parameters model := " + model + ", year := " + year));
}
@ManagedProperty("#{param.model}")
private String model;
@ManagedProperty("#{param.year}")
private int year;
public void setModel(String model) {
this.model = model; // value set by JSF
}
public void setYear(int year) {
this.year = year;
}
}
回答by jorelia
If you want to call your own function, eg. a confirm dialog, your custom function must be compliant to the passing parameter style. eg:
如果你想调用自己的函数,例如。确认对话框,您的自定义函数必须符合传递参数样式。例如:
<p:commandLink id="myId" onclick="confirmDelete([{name:'Id', value: '#{my.id}'}]);" immediate="true">
The java script function
java脚本函数
function confirmDelete(id) {
if (confirm('Do you really want to delete?')) {
remoteDeleteDemand(id);
return true;
}
The remoteCommand tag
远程命令标签
<p:remoteCommand name="remoteDeleteDemand" actionListener="#{myController.doDelete}" />
回答by David F. Suárez Chacón
When you need to pass more than one parameter from javascript, the syntax is:
当你需要从 javascript 传递多个参数时,语法是:
var param1= ...;
var param2= ...;
var param3= ...;
var param1= ...;
var param2= ...;
var param3= ...;
remoteCommandFunction([{name:'param1', value:param1}, {name:'param2',value:param2}, {name:'param3',value:param3}]);
remoteCommandFunction([{name:'param1', value:param1}, {name:'param2',value:param2}, {name:'param3',value:param3}]);
回答by Vasil Valchev
PrimeFace 5.0, dynamic array (all table column width will be send by this method)
PrimeFace 5.0,动态数组(所有表格列宽都会通过这种方式发送)
Beam
光束
public void updateTableColumnsWidth() {
FacesContext context = FacesContext.getCurrentInstance();
Map<String, String> map = context.getExternalContext().getRequestParameterMap();
}
p:remoteCommand
p:远程命令
<h:form>
<p:remoteCommand name="remoteCommand" action="#{controller.updateTableColumnsWidth}" />
</h:form>
JS
JS
<script type="text/javascript">
function updateTableColumnsWidth () {
var columnsCount = document.getElementById('table').rows[0].cells.length;
var json = [];
for (var i = 0; i < columnsCount; i++) {
json[i] = { name: i, value: document.getElementById('table').rows[0].cells[i].offsetWidth};
}
console.log(json);
remoteCommand(json);
};
</script>