X

The blog for hints & tips about Oracle Application Development Framework and Mobile Application Framework

  • ADFv
    December 3, 2010

How-to call server side Java from JavaScript

Frank Nimphius
Master Principal Product Manager

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

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.