Sunday, May 29, 2011

Call Java script from Java

Following is a sample code for calling javascript from java:


import org.apache.myfaces.trinidad.render.ExtendedRenderKitService; import org.apache.myfaces.trinidad.util.Service; …

private void writeJavaScriptToClient(String script)
{
  FacesContext fctx = FacesContext.getCurrentInstance();
  ExtendedRenderKitService erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class);  
  erks.addScript(fctx, script);
}

A popular use case for calling JavaScript from a managed bean is to open a client-side ADF Faces popup dialog, as shown here:

StringBuilder script = new StringBuilder();
script.append( "var popup = AdfPage.PAGE.findComponentByAbsoluteId('p1');");
script.append("if(popup != null){"); script.append("popup.show();"); script.append("}"); writeJavaScriptToClient(script.toString());

No comments:

Post a Comment