How-to call server side Java from JavaScript

Posted by frank.nimphius on Oracle Blogs See other posts from Oracle Blogs or by frank.nimphius
Published on Fri, 03 Dec 2010 21:46:25 +0000 Indexed on 2010/12/06 16:58 UTC
Read the original article Hit count: 581

Filed under:

The af:serverListener tag in Oracle ADF Faces allows JavaScript to call into server side Java. The example shown below uses an af:clientListener tag to invoke client side JavaScript in response to a key stroke in an Input Text field. The script then call a defined af:serverListener by its name defined in the type attribute. The server listener can be defined anywhere on the page, though from a code readability perspective it sounds like a good idea to put it close to from where it is invoked.

<af:inputText id="it1" label="...">
  <af:clientListener method="handleKeyUp" type="keyUp"/>
  <af:serverListener type="MyCustomServerEvent"
                     method="#{mybean.handleServerEvent}"/>
</af:inputText>

The JavaScript function below reads the event source from the event object that gets passed into the called JavaScript function. The call to the server side Java method, which is defined on a managed bean, is issued by a JavaScript call to AdfCustomEvent. The arguments passed to the custom event are the event source, the name of the server listener, a message payload formatted as an array of key:value pairs, and true/false indicating whether or not to make the call immediate in the request lifecycle.

<af:resource type="javascript">  
  function handleKeyUp (evt) {
   var inputTextComponen = event.getSource();   
   AdfCustomEvent.queue(inputTextComponent,
                        "MyCustomServerEvent ",
                        {fvalue:component.getSubmittedValue()},
                        false);
   event.cancel();} 
 </af:resource>

The server side managed bean method uses a single argument signature with the argument type being ClientEvent. The client event provides information about the event source object - as provided in the call to AdfCustomEvent, as well as the payload keys and values. The payload is accessible from a call to getParameters, which returns a HashMap to get the values by its key identifiers.

 public void handleServerEvent(ClientEvent ce){
   String message = (String) ce.getParameters().get("fvalue");
  ...
 }


Find the tag library at: http://download.oracle.com/docs/cd/E15523_01/apirefs.1111/e12419/tagdoc/af_serverListener.html

© Oracle Blogs or respective owner

Related posts about ADFv