【问题标题】:Cannot pass parameter from javascript to JSF Bean using primefaces RemoteCommand [duplicate]无法使用primefaces RemoteCommand将参数从javascript传递到JSF Bean [重复]
【发布时间】:2013-07-13 20:02:54
【问题描述】:

我有一个使用 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 始终为空,这意味着参数没有被传递。我已经阅读了其他帖子,但我不明白为什么它不会通过。我还尝试使用 JSF managedBean 作为支持 bean 和一个名为 bean 的 CDI,两者都有相同的结果。

【问题讨论】:

    标签: javascript jsf primefaces


    【解决方案1】:

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

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

    【讨论】:

    • 我正在使用 LeafletJS。当按照建议用双引号括起来时,javascript 会抛出错误 TypeError:t.addLayer is not a function。我尝试用这样的单引号括起来:scaleLines({'name':'newZoom','value':'10'});。这产生了与之前相同的结果:调用了 Bean 方法,但 NewZoom 为空。
    • 实际上,我从上面的评论中注意到我省略了括号。这似乎工作scaleLines([{'name':'newZoom','value':'10'}]);
    【解决方案2】:

    你能像scaleLines([{newZoom:'10'}])这样在scaleLines()的参数周围加上方括号[]吗?另外请确保remoteCommandh:form 内。如果不行,因为我很久没用primefaces了,自从上次用了很多东西都变了,你可以用传统的方式来交流js和backbean。即拥有1个文本字段和1个按钮的隐藏形式,并使用jquery/js将值10填充到文本字段的值。此文本字段应将其值绑定到 bean 中的 newZoom 变量。然后使用 jquery/js 点击会触发scaleLines 方法的命令按钮。如果您不必将文本字段绑定到 bean 上的变量,您仍然可以从 requestParameterMap 中获取值 10

    【讨论】:

      猜你喜欢
      • 2011-11-21
      • 2015-01-07
      • 2019-09-27
      • 1970-01-01
      • 2014-12-23
      • 2014-03-23
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      相关资源
      最近更新 更多