How-to call server side Java from JavaScript
- by frank.nimphius
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
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");
...
}
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
Find the tag library at: http://download.oracle.com/docs/cd/E15523_01/apirefs.1111/e12419/tagdoc/af_serverListener.html