获取Java Script的返回值到服务器端

时间:2014-10-27 02:44:34

标签: java javascript jsp jsf

我在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>

1 个答案:

答案 0 :(得分:0)

你有很少的方法可以从JS传递到支持Bean,我很害怕,但是所有这些都很好地描述了post

总结我最常用的方式(尽管我真的尝试设计以便我从不需要这个),帖子中描述的内容如下:

<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,您将获得解决方案