How to run javascript from ADF backing bean

By W.ZH Feb 2014
There are two ways can help on this topic:


1. You want to run a piece of the js from ADF backing bean, directly

        FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService service =
service.addScript(facesContext, “alert('my js showing');“);

2. You have a js function on the UI side already, when user click some button on the UI, you want you button also trigger this js method from the backing bean.

If you bind your UI component to the backing bean as the
HtmlCommandButton, not as the RichCommandButton, then setOnclick, setOndblclick, can help you to set them to run a javascript, and you also have chance to compose for your js function input.  I just copy code from this post to here:

1. Small JavaScript Function in JSPX Page which gives the alert message with Hitcount of Command Button

<script language=”JavaScript”>
function showHitCount(count) {
alert(“Hit Count : ” + count);

2.. A Command Button (JSF Component) in JSPX Page with Code

<h:commandButton id=”b1″
value=”Invoke JavaScript from Backing Bean”

3. One Backing Bean property for this command button in TestBean Backing Bean.

public class TestBean {

private HtmlCommandButton hitCount;
private int HitCount;
public TestBean() {
HitCount=0; }

public void setHitCount(HtmlCommandButton hitCount) {
System.out.println(“TestBean : setAlert();”);
hitCount.setOnclick(“showHitCount(” + (++HitCount) + “);”); this.hitCount = hitCount;

public HtmlCommandButton getHitCount() {
System.out.println(“TestBean : getAlert();”);
return hitCount;