0

I have a JSF site using primeFaces. I cannot get any parameters from my javascript to my bean using P:RemoreCommand. Here is my code:

xhtml:

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

Then later to call it:

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

This does call the method, but when I attach a debugger, I can see that newZoom is always null meaning that the parameter does not get passed. I hhave read other posts and I can't see why it would not get passed. I also tried this using a JSF managedBean for the backing bean and a CDI named bean, bOth had the same results.

Rhino
  • 101
  • 8

2 Answers2

0

The problem is that you are using the wrong format. You have to use an array of JS-objects with "name" and "value". I.e. [{"name":"param1","value":"value1"},...]

In your case changing the parameters of scaleLine to [{"name":"newZoom","value":"10"}] should do the trick. Additionally you should surround the key in the JS-object with "", otherwise it is no valid JSON.

user1983983
  • 4,793
  • 2
  • 15
  • 24
  • I am using LeafletJS. When surrounding in double quotes as suggested, the javascript throws the error TypeError:t.addLayer is not a function. I tried surrounding in single quotes like this: `scaleLines({'name':'newZoom','value':'10'});` . This produced the same result as before: The Bean method is called, but NewZoom is null. – Rhino Jul 16 '13 at 13:12
  • Actually , I noticed from my above comment that I had left out the brackets. This seems to work `scaleLines([{'name':'newZoom','value':'10'}]);` – Rhino Jul 16 '13 at 13:24
-1

Can you put a square bracket [] around the parameters of scaleLines() like this scaleLines([{newZoom:'10'}])? Also please make sure that remoteCommand is inside a h:form. If it does not work, as I have not used primefaces for a long time and lots have changed since I last used, you can just use tradition way to communicate js and backbean. That is to have a hidden form of 1 text field and 1 button, and use jquery/js to populate the value 10 to the value of the text field. This text field should bind its value to newZoom variable in your bean. Then use jquery/js to click the command button that will trigger scaleLines method. If you dont have to bind text field to a variable on your bean, you can still get the value 10 from requestParameterMap.

Thang Pham
  • 38,125
  • 75
  • 201
  • 285