将javascript变量传递给Java bean [英] Passing javascript variable to java bean
问题描述
我正在尝试从传单地图(javascript)获取坐标,并将其传递给我的托管bean.为了将javascript变量发送到托管bean,我使用了答案
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
这是我正在使用的代码 JSF:
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>
豆类:
//....
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是标记的坐标(传单标记) 正如Holger在评论中所说,该表单尚未提交,因此修改remoteCommand解决了我的问题.这是修改内容:
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()}" />
推荐答案
您不需要表格和所有这些东西.
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 }
]);
};
和服务器端
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String latt = params.get("latt");
String longt = params.get("longt");
这篇关于将javascript变量传递给Java bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!