如何将JavaScript值传递给JSF EL和支持bean? [英] How to pass JavaScript values to JSF EL and backing bean?
问题描述
我正在做JSF地理定位服务,我需要将经纬度传递给bean进行处理。 HTML5允许使用JavaScript获取位置信息,例如 http://code.google的.com / p /地理位置的JavaScript / 。
将以下代码放到JSF页面中,显示带有GPS坐标的警报
< script>
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true,options:5000});
} else {
alert(功能不可用);
}
函数success_callback(p){
alert('lat ='+ p.coords.latitude.toFixed(2)+'; lon ='+ p.coords.longitude.toFixed (2));
}
函数error_callback(p){
alert('error ='+ p.message);
}
< / script>
如何使用 p.coords.latitude.toFixed(2) code> value将它传递给
h:inputtext
组件?
< h:inputText>
,您会看到类似于 < input type =textid =formid:inputid/>
在JS中,您可以使用文档轻松地从HTML DOM中获取HTML元素
函数并修改它。
var input = document.getElementById('formid:inputid');
input.value ='new value';
另请参阅:
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>
How to use p.coords.latitude.toFixed(2)
value to pass it for example to h:inputtext
component?
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" />
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';
See also:
这篇关于如何将JavaScript值传递给JSF EL和支持bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!