有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将javascript变量传递给JavaBean

我试图从传单映射(javascript)中获取坐标,并将它们传递给我的托管bean。为了向我的托管bean发送javascript变量,我使用了答案here

这是我正在使用的代码 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>

豆子:

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

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

}
 //getters and setters

我没有得到错误,但latt和longt的值始终为0。 ps:latt和longt是标记(传单标记)的坐标 编辑:正如霍尔格在评论中所说,表单没有提交,因此修改remoteCommand解决了我的问题。以下是修改内容:

<p:remoteCommand name="myRemote" process="@form" actionListener="#{mapbean.displayLatLong()}"  />

共 (1) 个答案

  1. # 1 楼答案

    你不需要表格和所有这些东西

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

    在服务器端

    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    
    String latt  = params.get("latt");
    String longt = params.get("longt");