Two ways of triggering the JavaScript execution
The RequestContext
utility class provides an easy way to execute any JavaScript code after the current AJAX request completes. The JavaScript block has to be coded in Java and can be executed by passing it to the execute()
method. An alternative approach would be to update a script block on a page and trigger the script execution manually. In this case, the JavaScript block is coded directly into a page.
In this recipe, we will see both solutions for JavaScript execution. For this purpose, we will develop a menu
component and toggle the enabling/disabling of menu items with two command buttons. The first command button should toggle enabling/disabling with the server-side approach and the second one with the client-side approach.
How to do it…
Let's write a p:menu
tag with three menu items. We also need two p:commandButton
tags with appropriate action listeners:
<h:outputText id="indicator" value="Enabled? - #{javaScriptExecBean...