Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

08 September, 2013

Run JavaScript from Native Java

I wrote in previous post Execute Javascript code from Java Code how to execute java script in Oracle ADF.
But in this post I will explain how to call Javascript from native Java.



The following code snippets will illustrate how to evaluate Javascript code and invoke functions and get return value.

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class CallJavaScript {
    public static void callJsCode(String jsCode) throws ScriptException,
                                                        NoSuchMethodException {

        // Retrieving the Javascript engine
        ScriptEngine se =
            new ScriptEngineManager().getEngineByName("javascript");
        try {
            se.eval(jsCode);
        } catch (ScriptException e) {
            e.printStackTrace();
        }

        try {
            Invocable jsinvoke = (Invocable)se;
            System.out.println("myFunction(2) returns: " +
                               jsinvoke.invokeFunction("myFunction", 2, 4));
        } catch (ScriptException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }
}

Then You can call this code using for example
       try {
            String jsCode =
                "function myFunction(x,y){return x+y;}";
            CallJavaScript.callJsCode(jsCode);
        } catch (ScriptException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }      

The output of previous code will be

myFunction(2) returns: 6.0

Thanks

09 November, 2012

Execute Code in Page Load in ADF


I want to execute a piece of code in page load, For implementing this request I can do it using two solutions.

1- PagePhaseListener Interface
PagePhaseListener allows to write global code which executes in every page at my application.
I will create a class implements oracle.adf.controller.v2.lifecycle.PagePhaseListener.PagePhaseListener Interface and overide afterPhase method and then add this class as Phase Listener in  /META-INF/adf-settings.xml

2- BeforePhase Property of View
I will bind this property to a method in backing bean which contains the code I want to execute.

03 October, 2012

Execute Javascript code from Java Code

In ADF framework you can execute Javascript code from Java code using the below method

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

Import the following classes
 import javax.faces.context.FacesContext;  
 import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;  
 import org.apache.myfaces.trinidad.util.Service;  


You can call previous method from anywhere from your code
For example I will display alert using javascript
 runJavaScriptCode("alert(\"My name is Mahmoud\");");  

Thanks

12 June, 2012

ADF : Scope Variables

Oracle ADF uses many variables and each variable has a scope. There are five scopes in ADF (Application, Request, Session, View and PageFl...