Wednesday, February 16, 2011

Passing Parameters from JavaScript to Manage/Backing bean

Sometimes you cannot avoid using JavaScript even if that is not the very recommended approach with ADF. However when doing the same there is often need to pass values from java script function to bean. If you got yourself in scenario like this.. here is how you can do it.

In your JavaScript method you can assign the values to param and pass it to the event.

function myClientListener(event){
var element = event.getSource();
var param = {city:"New York", country:"US"};
AdfCustomEvent.queue(element, "jsServerListener", param, true);
event.cancel();
}

In your page associate this event to a handler method from java bean

<af:serverListener type="jsServerListener"
method="#{MyBean.serverEventHandler}"/>


Now in bean here is how you can get access to it:

public void serverEventHandler(ClientEvent clientEvent) {
System.out.println(clientEvent.getParameters().get("city"));
System.out.println(clientEvent.getParameters().get("country"));
}

No comments:

Post a Comment