使用 JavaScript 设置 f:param 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5579751/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:40:18  来源:igfitidea点击:

Set f:param value with JavaScript

javajavascriptjsf

提问by sergionni

Is it possible to do:

是否可以这样做:

jsf code (pseudo):

jsf代码(伪):

...
<f:param name="arg" value="document.getElementById('naming').text()">
<h:inputText id="naming"></h:inputText>
...

I mean approach,when <f:param>is set with JS.

我的意思是方法,什么时候<f:param>用 JS 设置。

Is it bad practice?

这是不好的做法吗?

Thanks for help.

感谢帮助。

采纳答案by Yuri Ghensev

You need to use a4j's commandButtonand actionParamto be able to pass a dynamic param back to the server.

您需要使用 a4jcommandButtonactionParam能够将动态参数传递回服务器。

Additionally, you need an attribute on your bean that will receive the param value.

此外,您的 bean 上需要一个属性来接收参数值。

Example:

例子:

<a4j:commandButton action="#{myBean.action}" value="Submit!">
    <a4j:actionParam name="arg" noEscape="true" value="getTheValue()" assignTo="#{myBean.myBeanArg}" />
</a4j:commandButton>

Here myBean.myBeanArgwill receive the value returned by the javascript function getTheValue().

这里myBean.myBeanArg将接收 javascript 函数返回的值getTheValue()

Notice the noEscape="true"attribute. This is needed because otherwise the data inside valuewould be enclosed in single quotes and escaped, resulting in no javascript execution. As stated in the documentation:

注意noEscape="true"属性。这是必需的,因为否则里面的数据value会被单引号括起来并被转义,导致没有 javascript 执行。如文档中所述:

It is possible to use JavaScript expression or function in the "value" attribute. In this case the "noEscape" attribute should be set to "true". The result of this JavaScript invocation is sent to the server as a value of <a4j:actionparam>.

可以在“value”属性中使用 JavaScript 表达式或函数。在这种情况下,“noEscape”属性应设置为“true”。此 JavaScript 调用的结果作为 值发送到服务器<a4j:actionparam>

回答by Jigar Joshi

<f:param>is server side stuff while javascript is client side. So you can't

<f:param>是服务器端的东西,而 javascript 是客户端。所以你不能

You can use ajax a4jto do this ,

您可以使用 ajaxa4j来做到这一点,

回答by Hache

No you can't. You could change an attribute of a link for example and get this attribute in your action method ont he server side.

不,你不能。例如,您可以更改链接的属性,并在服务器端的操作方法中获取此属性。

Alternatively you could use an hidden input field linked to an attribute in your bean.

或者,您可以使用链接到 bean 中的属性的隐藏输入字段。