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()}"  />
4
  • Who calls the onClick()? Commented May 9, 2017 at 9: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); Commented May 9, 2017 at 9: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. Commented May 9, 2017 at 9:47
  • yes I think this is the problem! no post request is generated. I will try to make my remotecommand submit the form. Commented May 9, 2017 at 10:18

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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);
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
please modify onClick() to onClick(e)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.