如何将 JavaScript 值传递给 JSF EL 和支持 bean?

2022-01-14 00:00:00 geolocation javascript html el jsf

我正在做 JSF 地理定位服务,我需要将纬度和经度传递给 bean 进行处理.HTML5 允许使用 JavaScript 获取位置,例如在 http://code.google.com/p/geo-location-javascript/.将以下代码放入 JSF 页面会显示带有 GPS 坐标的警报

I am doing JSF geolocation service where I need to pass latitude and longitude to bean for processing. HTML5 allows getting location with JavaScript, for example like is done in http://code.google.com/p/geo-location-javascript/. Putting following code to JSF page shows alert with GPS coordinates

<script>
    if (geo_position_js.init()) {
        geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true,options:5000});
    } else {
        alert("Functionality not available");
    }
    function success_callback(p) {
        alert('lat='+p.coords.latitude.toFixed(2)+';lon='+p.coords.longitude.toFixed(2));
    }

    function error_callback(p) {
        alert('error='+p.message);
    }
</script>

如何使用 p.coords.latitude.toFixed(2) 值将其传递给例如 h:inputtext 组件?

How to use p.coords.latitude.toFixed(2) value to pass it for example to h:inputtext component?

推荐答案

你需要意识到 JSF 在 webserver 上运行并产生一堆 HTML/CSS/JS 代码从 webserver 发送到 webbrowser 并且 webbrowser 只运行HTML/CSS/JS.右键单击 webbrowser 中的页面并选择查看源代码.代替 <h:inputText> 你会看到类似

You need to realize that JSF runs at webserver and produces a bunch of HTML/CSS/JS code which get sent from webserver to webbrowser and that the webbrowser only runs HTML/CSS/JS. Rightclick the page in webbrowser and choose View Source. In place of the <h:inputText> you'll see something like

<input type="text" id="formid:inputid" />

在 JS 中,您可以使用 document 函数轻松地从 HTML DOM 中获取 HTML 元素并对其进行修改.

In JS, you can easily grab HTML elements from the HTML DOM using document functions and alter it.

var input = document.getElementById('formid:inputid');
input.value = 'new value';

另见:

  • Java/JSP/JSF与JavaScript之间的通信

相关文章