如何将JavaScript值传递给JSF EL和支持bean? [英] How to pass JavaScript values to JSF EL and backing bean?

查看:148
本文介绍了如何将JavaScript值传递给JSF EL和支持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 组件?

解决方案您需要认识到,JSF运行在web服务器上,并生成一系列HTML / CSS / JS代码,它们从web服务器发送到webbrowser,并且webbrowser只运行HTML / CSS / JS。在浏览器中右键单击页面,然后选择查看源代码。取代< 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆