【问题标题】:Get the returned value of Java Script to server side获取 Java Script 的返回值到服务器端
【发布时间】:2014-12-22 05:04:22
【问题描述】:

我正在学习 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>

【问题讨论】:

  • 不是很清楚你在问什么。您是否在问如何将当前位置从您的网页发送回您的服务器?
  • 完全正确!现在我现在得到Loc------------->javax.faces.component.html.HtmlInputHidden@623823bc 但我想看到“***Latitude:” + position.coords.latitude + "***经度:" + position.coords.longitude;当我打电话给 FirstBean.nowLoc
  • 您需要使用 ajax 调用将数据发送到您的服务器。在您的服务器上创建一个端点,然后使用 ajax 调用将数据发送到该端点。
  • 你有没有打电话给getLocation()?为什么元素 ID 中有特殊字符?
  • 在我调用 getLocarion() 的脚本中

标签: java javascript jsp jsf


【解决方案1】:

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

【讨论】:

  • 我还不能工作 JS 点击并提交。无论如何,我可以在没有按钮或链接的情况下完成操作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
相关资源
最近更新 更多