Passing parameters from JSF to JS (server to client)

Reversing the preceding recipe, takes the form of passing variables from JSF to JS (from server to client).

Getting ready

We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...

The solution is straightforward, and it is presented next (we have passed a constant and an EL result to a JS function, using the onload event with the h:body task:

<script type="text/javascript" language="javascript">
function variableFromServer(variable){
alert("Variable from server: " + variable)
}
</script>
…
<!-- passing a constant -->
<h:body onload="variableFromServer('JSF_1'),">
<!-- passing the EL result -->
<h:body onload="variableFromServer('#{bean.text}'),">

How it works...

I think that the previous code is self explanatory!

There's more...

We passed a variable using the onload event, but this is just an example. You can follow this example to use any other events and conjunctures, accordingly to your application's needs.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Pass_param_from_JSF_to_JS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset