How to pass JavaScript values to JSF EL and backing bean?(如何将 JavaScript 值传递给 JSF EL 和支持 bean?)
问题描述
我正在做 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之间的通信
这篇关于如何将 JavaScript 值传递给 JSF EL 和支持 bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 JavaScript 值传递给 JSF EL 和支持 bean?


基础教程推荐
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01