Thursday, November 25, 2010

Executing Javascript in an ADF RC Page from a backing bean method

ADF lets us do some pretty wild things and one of them is the ability to easily inject Javascript into a page from a backing bean method and execute it. For example, from the action assciated with a command button, to run run a simple JavaScript the only code required is
FacesContext fctx = FacesContext.getCurrentInstance();
StringBuilder script = new StringBuilder();
script = script.append("alert('Hello World !');");
Service.getRenderKitService(fctx,ExtendedRenderKitService.class)
.addScript(fctx,script.toString());

As can be seen, the backing bean code for executing java script in an ADF page is very simple and makes use of the ExtendedRenderKitService from the Trinidad codebase. The approach becomes very useful when you need to generate dynamic JavaScript to inject and execute it in a page at runtime.

No comments:

Post a Comment