从JavaScript传递参数到p:remoteCommand

问题描述 投票:41回答:7

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

javascript jsf primefaces parameters remotecommand
7个回答
23
投票
remoteCommandFunctionName({name1:'value1', name2:'value2'});

79
投票

对的,这是可能的。如何做到这一点取决于PrimeFaces版本。你可以在PrimeFaces users guide看到它。在PrimeFaces 3.3版之前,语法如下(从3.2用户指南中复制):

3.80 RemoteCommand

...

Passing Parameters

远程命令可以通过以下方式发送动态参数;

increment({param1:'val1', param2:'val2'});

它通过常规方式在支持bean中提供。例如。在请求范围的bean中:

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

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

或者在更广泛的范围豆的方法中:

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

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


从PrimeFaces版本3.3开始,语法如下(从3.3用户指南中复制):

3.81 RemoteCommand

...

Passing Parameters

远程命令可以通过以下方式发送动态参数;

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>完全无关)

或者在更广泛的范围豆的方法中:

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;

或者在更广泛的范围豆的方法中:

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

更新

访问现代CDI JSF中的参数,可以用不同的方式完成,在this Stackoverflow Q/A中概述


57
投票

页:

<p:remoteCommand name="command" action="#{bean.method}" />

JavaScript的:

command({param: 'value'});

豆:

public void method() {
    String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("param");
}

8
投票

将@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;
    }
}

4
投票

当您需要从javascript传递多个参数时,语法为:


var param1 = ...;
var param2 = ...;
var param3 = ...;

remoteCommandFunction([{name:'param1',value:param1},{name:'param2',value:param2},{name:'param3',value:param3}]);


3
投票

如果你想调用自己的功能,例如。在确认对话框中,您的自定义函数必须符合传递参数样式。例如:

   <p:commandLink id="myId" onclick="confirmDelete([{name:'Id', value: '#{my.id}'}]);" immediate="true">

javascript函数

            function confirmDelete(id) {
            if (confirm('Do you really want to delete?')) {
                remoteDeleteDemand(id);
                return true;
            }

remoteCommand标签

<p:remoteCommand name="remoteDeleteDemand" actionListener="#{myController.doDelete}" />

1
投票

PrimeFace 5.0,动态数组(所有表格列宽都将通过此方法发送)

光束

public void updateTableColumnsWidth() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, String> map = context.getExternalContext().getRequestParameterMap();
}

电话号码:remoteCommand

<h:form>
     <p:remoteCommand name="remoteCommand" action="#{controller.updateTableColumnsWidth}" />
</h:form>

JS

<script type="text/javascript">
        function updateTableColumnsWidth () {
                var columnsCount = document.getElementById('table').rows[0].cells.length;

                var json = [];

                for (var i = 0; i &lt; columnsCount; i++) {
                    json[i] = { name: i, value: document.getElementById('table').rows[0].cells[i].offsetWidth};

                }

                console.log(json);
                remoteCommand(json);
            };
    </script>
© www.soinside.com 2019 - 2024. All rights reserved.