sergionni
sergionni

Reputation: 13520

Set f:param value with JavaScript

Is it possible to do:

jsf code (pseudo):

...
<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.

Is it bad practice?

Thanks for help.

Upvotes: 6

Views: 11187

Answers (3)

Yuri Ghensev
Yuri Ghensev

Reputation: 2545

You need to use a4j's commandButton and actionParam to be able to pass a dynamic param back to the server.

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

Example:

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

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

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

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>.

Upvotes: 6

Hache
Hache

Reputation: 449

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.

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240928

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

You can use ajax a4j to do this ,

Upvotes: 4

Related Questions