Primefaces和Remote Command:处理JavaScript参数的最佳实践

时间:2012-12-03 19:05:26

标签: javascript jquery jsf jsf-2 primefaces

我已经定义了两个RemoteCommand:

<p:remoteCommand name="rc1" actionListener="#{rcBean.rcActionListener1}" action="#{rcBean.rcAction1}" />

<p:remoteCommand name="rc2" action="#{rcBean.rcAction2}" />

Javascript方法使用如下参数调用rc1和rc2:

rc1({a:'value for a', b:'value for b'});

rc2({a:'value for a', b:'value for b'});

rcBean rcActionListener和rcAction是:[rcBean部分代码]

protected String param_a, param_b;

protected void processRcParams() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map map = context.getExternalContext().getRequestParameterMap();
    param_a = (String) map.get("a");
    param_b = (String) map.get("b");
}

public void rcActionListener1() {
    processRcParams();
}

public void rcAction1() {
    //-> parameters setted
    //-> process something...
}

public void rcAction2() {
    //-> parameters not set yet, and so
    processRcParams();
    //-> process something...
}

判断参数未直接在p:remoteCommand中定义(它们是通过rc1或rc2从JavaScript传递的),如果参数可以直接在动作中读取,则不需要actionListener。

最佳做法是什么:在actionListener之前或直接在动作中读取参数?为什么?

1 个答案:

答案 0 :(得分:1)

<script type="text/javascript">
            //<![CDATA[
            function test(){
                document.getElementById('hiddenurlpage').value = "test1";
                lazyload();

            }
                //]]>
        </script>
<h:inputHidden id="hiddenurlpage" value="#{userBean.currentpage}"/>
<p:remoteCommand name="lazyload"  process="@this,hiddenurlpage" />