无法使用primefaces RemoteCommand将参数从javascript传递到JSF Bean

时间:2013-07-15 20:11:48

标签: javascript jsf primefaces

我有一个使用primeFaces的JSF网站。我无法使用P:RemoreCommand从我的javascript到我的bean获取任何参数。这是我的代码:

XHTML:

<p:remoteCommand name="scaleLines" actionListener="#{mapBean2.scaleLines}"  update="mapPanel"/>

然后再称它为:

map.on('viewreset', function(){  
          scaleLines({newZoom:'10'});


           });

Session Scoped ManagedBean:

public void scaleLines(){

   String newZoom = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("newZoom");

    if(newZoom == null){
        Logger.getAnonymousLogger().info("Zoom level is null");
    }
    else{
    Integer newZoomInt = Integer.parseInt(newZoom);
    this.mapzoomLevel = newZoomInt;
    for(ZsyMap1Linetest line : allTestLines){
        line.setWeight(((line.getWeight()*newZoomInt)/(6)));
    }
   }
}

这会调用该方法,但是当我附加调试器时,我可以看到newZoom始终为null意味着该参数未被传递。我已经阅读了其他帖子,我不明白为什么它不会通过。我还尝试使用JSF managedBean作为支持bean和一个名为bean的CDI,bOth具有相同的结果。

2 个答案:

答案 0 :(得分:0)

问题是您使用的格式错误。您必须使用具有“name”和“value”的JS对象数组。即[{"name":"param1","value":"value1"},...]

在您的情况下,将scaleLine的参数更改为[{"name":"newZoom","value":"10"}]应该可以解决问题。另外,你应该用""包围JS对象中的键,否则它不是有效的JSON。

答案 1 :(得分:-1)

您可以在[]的{​​{1}}参数周围放置一个方括号scaleLines()吗?另请确保scaleLines([{newZoom:'10'}])位于remoteCommand内。如果它不起作用,因为我很长一段时间没有使用过primefaces,并且自从我上次使用以来已经有很多变化,你可以使用传统方式来传递js和backbean。这是隐藏形式的1个文本字段和1个按钮,并使用jquery / js将值h:form填充到文本字段的值。此文本字段应将其值绑定到bean中的10变量。然后使用jquery / js单击将触发newZoom方法的命令按钮。如果您不必将文本字段绑定到bean上的变量,您仍然可以从requestParameterMap获取值scaleLines