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.
- 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; - And to call the javsccript from the java code :
runJavaScriptCode("alert(\"My name is Java\");");
No comments:
Post a Comment