我正在学习JSF我正试图将当前位置从JS获取到JSf,然后将其传递回FirstBean.nowLoc-bean。我尝试了很多选择,但没有成功:(有人能帮我吗?
<h:message id="errors" for="User_ID" style="color:red" />
<h:form id="getLoc">
<h3>
<h:inputHidden id="xxx" binding="#{FirstBean.nowLoc}" />
</h3>
</h:form>
<p>
<h:outputText id="c"
value="currentLocation ----- #{FirstBean.currentLocation }"/>
<h:outputText id="x" value="nowLoc-------------->#{FirstBean.nowLoc}"/>
<script>
getLocation();
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
loc = "***Latitude: " + position.coords.latitude
+ "***Longitude: " + position.coords.longitude;
document.getElementById("getLoc:xxx").value = loc;
return loc;
}
</script>
您有几种从JS传递到backing Bean的方法,恐怕都很难看,但在这篇文章中描述得很好
总结一下我使用最多的方式(尽管我真的试图设计,这样我就永远不需要这个了),帖子中描述的内容如下
<h:form id="formId">
<h:inputText id="inputId" value="#{bean.property}" />
<h:commandButton id="buttonId" value="Button" action="#{bean.action}" />
</h:form>
因此,JSF表单(通常是不可见的),以及一段js代码
<script type="text/javascript">
document.getElementById('formId:inputId').value = 'foo';
document.getElementById('formId:buttonId').click();
// Or: document.getElementById('formId').submit();
</script>
因此,如果您无法避免这种情况,请传入值,并以您的bean为目标,您将获得解决方案