Tuesday 11 June 2013

ADF : Running or calling javascript from java bean

In the other post of mine I wrote about the steps to call Java method from a javascript code. Now I am gonna tell you how we can call a Javascript code from Java code. This is the easiest method I found on the internet.
  1. For calling Javascript you have to use the following method in the java bean :

    public static void runJavaScriptCode(String javascriptCode) { 
             FacesContext facesCtx = FacesContext.getCurrentInstance(); 
             ExtendedRenderKitService service =
    Service.getRenderKitService(facesCtx, ExtendedRenderKitService.class); 
             service.addScript(facesCtx, javascriptCode); 
           } 



    You have to import the following packages :
    import javax.faces.context.FacesContext;
    import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
    import org.apache.myfaces.trinidad.util.Service;
  2.  And to call the javsccript from the java code :
    runJavaScriptCode("alert(\"My name is Java\");");

No comments:

Post a Comment