javascript - Value inside JSF attribute tag - Stack Overflow

admin2025-04-20  0

How can I pass JS value to attribute nested tag inside ponent?

I have this code:

<p:remoteCommand .....>
    <f:attribute name="galaxie" value="jstest()" />
</p:remoteCommand>

And my simple JS jstest function :

function jstest(){
    return "foo";
}

When I test attribute value for galaxie in backing bean I have jstest() not foo.

How can I pass JS value to attribute nested tag inside ponent?

I have this code:

<p:remoteCommand .....>
    <f:attribute name="galaxie" value="jstest()" />
</p:remoteCommand>

And my simple JS jstest function :

function jstest(){
    return "foo";
}

When I test attribute value for galaxie in backing bean I have jstest() not foo.

Share Improve this question edited Dec 11, 2012 at 12:32 Xavi López 27.9k11 gold badges99 silver badges163 bronze badges asked Dec 10, 2012 at 14:28 Olivier J.Olivier J. 3,17511 gold badges49 silver badges73 bronze badges 2
  • Attributes of a ponent are declared and set at the server level, not at the client level. In other words, one does not submit a client side value to a server ponent attribute in this way. What is it that you are specifically trying to achieve because there is probably a better way to achieve it. – maple_shaft Commented Dec 10, 2012 at 14:42
  • thank you, yes I thought it... – Olivier J. Commented Dec 10, 2012 at 15:59
Add a ment  | 

1 Answer 1

Reset to default 6

The <f:attribute> is a JSF tag which runs in the webserver during producing HTML code. JavaScript is a client side language which doesn't run in webserver, but runs in the webbrowser after it has retrieved all the JSF-produced HTML code. Yet you seem to expect that they run "in sync". This is thus not true.

To achieve what you've had in mind, you basically need to provide <h:inputHidden> which is bound to a bean property and let JS fill it before the remote mand request is been fired.

E.g.

<h:form id="form">
    <h:inputHidden id="galaxie" value="#{bean.galaxie}" />
    <p:remoteCommand ... onstart="$('#form\\:galaxie').val(jstest())" process="@form" ... />
</h:form>

Alternatively, much easier is to just pass it as remote mand function argument which accepts a JS object representing the request parameter map. Given a

<h:form>
    <p:remoteCommand name="foo" ... />
</h:form>

you could just do:

foo({ galaxie: jstest() });

You can collect it by @ManagedProperty or ExternalContext#getRequestParameterMap().


Update: since PrimeFaces 3.3, the syntax for parameters in <p:remoteCommand> function has changed. If you're using at least PrimeFaces 3.3, then the function call should look like this:

foo([{ name: 'galaxie', value: jstest() }]);

See also Pass parameter to p:remoteCommand from JavaScript.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745105859a285286.html

最新回复(0)