gpt4 book ai didi

javascript - 将参数传递给 p :remoteCommand from JavaScript

转载 作者:IT王子 更新时间:2023-10-29 03:03:55 24 4
gpt4 key购买 nike

我想从 javascript 向 remoteCommand 传递值。如果可能的话,我该怎么做以及如何在支持 bean 中接收它们?

最佳答案

是的,这是可能的。如何做到这一点取决于 PrimeFaces 版本。你可以在 PrimeFaces users guide 中看到它.

PrimeFaces 3.3 或更新版本

自 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}]);

这种方式提供了在单个参数名称上指定多个值的可能性。具有上述单个值的参数的可用方式与旧方式相同:

@ManagedProperty("#{param.x}")
private int x;

@ManagedProperty("#{param.y}")
private int y;

(注意:您可以在 Mojarra 中使用 Integer,但不能在 MyFaces 中使用,这与 <p:remoteCommand> 完全无关)

或在范围更广的 bean 方法中:

Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
int x = Integer.valueOf(params.get("x"));
int y = Integer.valueOf(params.get("y"));

如果你需要指定一个参数有多个值,那么你可以这样做:

functionName([{name:'foo', value:'one'}, {name:'foo', value:'two'}, {name:'foo', value:'three'}]);`

在请求作用域的 bean 中:

@ManagedProperty("#{paramValues.foo}")
private String[] foos;

或在范围更广的 bean 方法中:

Map<String, String[]> paramValues = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap();
String[] foos = paramValues.get("foo");

PrimeFaces 3.2 或更早版本

之前 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'});

它可以通过通常的方式在辅助 bean 中使用。例如。在请求范围的 bean 中:

@ManagedProperty("#{param.param1}")
private String param1;

@ManagedProperty("#{param.param2}")
private String param2;

或在范围更广的 bean 方法中:

Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String param1 = params.get("param1");
String param2 = params.get("param2");

然而,这种方法的缺点是您不能像普通 HTML 表单和 HTTP 请求参数那样指定具有多个值的单个参数(在现实世界中用于例如多选下拉列表和多选复选框组)。


另见:

关于javascript - 将参数传递给 p :remoteCommand from JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com