0

I am trying to get coordinates from leaflet map ( javascript) and passing them to my managed bean. In order to send javascript variable to my managed bean, I used the answer here

here is the code I'm using the JSF :

    <h:form id="formId">

        <h:inputHidden id="x" value="#{mapbean.latt}" />
        <h:inputHidden id="y" value="#{mapbean.longt}" />

        <p:remoteCommand name="myRemote" action="#{mapbean.displayLatLong()}" immediate="true" />
    </h:form>

<script type="text/javascript">
function onClick(e) {   

     var  ff = e.latlng.lat;
          ff2= e.latlng.lng;


     document.getElementById("formId:x").value = ff;
     document.getElementById("formId:y").value = ff2;                   
     myRemote();        

    } 

</script>

The bean :

//....
public int               latt;
public int               longt;
public void displayLatLong(){

        System.out.println("x: " + latt);
        System.out.println("y: " + longt);

}
 //getters and setters

I'm not getting errors, but the value of latt and longt are always 0. ps :latt and longt are coordinates of a marker ( leaflet marker) Edit : as Holger said in his comment, the form was not submitted,so modifying the remoteCommand solved my problem. here are the modifications :

<p:remoteCommand name="myRemote" process="@form" actionListener="#{mapbean.displayLatLong()}"  />
Community
  • 1
  • 1
DarkSide
  • 25
  • 1
  • 9
  • Who calls the `onClick()`? – Holger May 09 '17 at 09:33
  • it's a click listener on the markers.. I am defining it in my bean by the following command : `RequestContext.getCurrentInstance().execute("layer.on('click', onClick)");` The onClick is working, I tested it by displaying an alert ( added this line of code to onClick : `alert(" lat/long : " + x+","+y);` – DarkSide May 09 '17 at 09:42
  • You should look in your browser console if the call generates a POST request. I don't think so, because you call remoteCommand and noone does a form submit. – Holger May 09 '17 at 09:47
  • yes I think this is the problem! no post request is generated. I will try to make my remotecommand submit the form. – DarkSide May 09 '17 at 10:18

1 Answers1

1

You don't need a form and all this stuff.

<p:remoteCommand name="myRemote" partialSubmit="true" process="@this"  action="#{mapbean.displayLatLong()}"/>

function onClick(e) {
  myRemote([
    { name: 'latt',  value: e.latlng.lat },
    { name: 'longt', value: e.latlng.lng }
  ]);
};

and at server side

Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

String latt  = params.get("latt");
String longt = params.get("longt");
Holger
  • 899
  • 2
  • 7
  • 12
  • I'll give it a try! but I have a question : How can the latt, longt values be updated in the server side ? I didn't get the params.get("..") role exactly. do you mean by the server side : the managed bean? – DarkSide May 09 '17 at 10:22
  • params is the parameter list of the call. all parameters of the call to myRemote() can be extracted with `params.get("usedname")`. After this you do a `int lattval = Integer.parseInt(latt);` – Holger May 09 '17 at 10:28
  • your proposition seems to be interesting to test.. however your first comment was right, there were no POST method, so the value of the parameter was not sent to the server, I will update my post with the modifications I did. Thanks again – DarkSide May 09 '17 at 13:32
  • please modify `onClick()` to `onClick(e)` – The Bitman May 09 '17 at 17:16