如何将 JavaScript 变量作为参数传递给 JSF 动作方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28591301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass JavaScript variables as parameters to JSF action method?
提问by Bubble
I'm preparing some variables in JavaScript (in my specific case, I'd like to get GPS location):
我正在用 JavaScript 准备一些变量(在我的特定情况下,我想获取 GPS 位置):
function getVars() {
// ...
var x = locationInfo.lng;
var y = locationInfo.lat;
}
I'd like to send them to my managed bean via the below command button:
我想通过以下命令按钮将它们发送到我的托管 bean:
<h:commandButton value="submit" onclick="getVars()" action="#{bean.submit(x,y)}" />
public void submit(int x, int y) {
// ...
}
How can I send xand yvariables from JavaScript to JSF managed bean action method?
我如何发送x和y从JavaScript到JSF变量托管bean的操作方法?
回答by BalusC
Let JS set them as hidden input values in the same form.
让 JS 将它们设置为相同形式的隐藏输入值。
<h:form id="formId">
<h:inputHidden id="x" value="#{bean.x}" />
<h:inputHidden id="y" value="#{bean.y}" />
<h:commandButton value="submit" onclick="getVars()" action="#{bean.submit}" />
</h:form>
function getVars() {
// ...
var x = locationInfo.lng;
var y = locationInfo.lat;
document.getElementById("formId:x").value = x;
document.getElementById("formId:y").value = y;
}
The command button action method could just access them as bean properties the usual way.
命令按钮操作方法可以以通常的方式将它们作为 bean 属性访问。
private int x;
private int y;
public void submit() {
System.out.println("x: " + x);
System.out.println("y: " + y);
// ...
}
// Getters+setters.
An alternative is to use OmniFaces<o:commandScript>or PrimeFaces<p:remoteCommand>instead of <h:commandButton>. See also a.o. How to invoke a JSF managed bean on a HTML DOM event using native JavaScript?
另一种方法是使用OmniFaces<o:commandScript>或PrimeFaces<p:remoteCommand>而不是<h:commandButton>. 另请参阅 ao如何使用本机 JavaScript 在 HTML DOM 事件上调用 JSF 托管 bean?
回答by Jose Manuel Gomez Alvarez
If you use PrimeFaces, you can use a hidden input field linked to a managed bean, and you can initialize its value using javascript, for PrimeFaces, the PFfunction can be used to access a widget variable linked to the hidden input, in this way:
如果使用PrimeFaces,则可以使用链接到托管 bean 的隐藏输入字段,并且可以使用 javascript 初始化其值,对于 PrimeFaces,PF函数可用于访问链接到隐藏输入的小部件变量,以这种方式:
<script type="text/javascript">
function getVars() {
// ...
var x = locationInfo.lng;
var y = locationInfo.lat;
PF('wvx').jq.val( lat1 );
PF('wvy').jq.val( lng1 );
}
</script>
<p:inputText type="hidden" widgetVar="wvx" value="#{bean.x}" />
<p:inputText type="hidden" widgetVar="wvy" value="#{bean.y}" />
回答by jonathan gonzalez
<h:form id="formId">
<h:inputHidden id="x" value="#{bean.x}" />
<h:inputHidden id="y" value="#{bean.y}" />
<h:commandButton value="submit" onclick="getVars()" action="#{bean.submit}" />
<script>
function getVars() {
var x;
var yt;
x=#{bean.x};
y=#{bean.y};
}
</script>

