How to Send Custom Events from the Client to the Server

Handling Events 5-17 optimally exchanged between JavaScript on the client end and Java on the server end. When the client is browser-based, the two common strategies for marshalling are JavaScript Object Notation JSON and XML. ADF Faces uses a mix of both of these strategies, with the information sent from the server to the client mostly as JSON and information sent from the client to the server as XML for more information about JSON, see http:www.json.org . When you send information from JavaScript to Java, the JavaScript data objects are converted marshalled into XML, which is then parsed back or unmarshalled into Java objects at the server-side. For example, consider a JSF page that has a commandButton component whose ID is cmd. When a user clicks the commandButton component, the client must communicate to the server that an actionEvent has been fired by this specific commandButton. In the requestParameter map, the information is mapped with the key using the format event + . + id where id is the ID of the component. So the requestParameter map key for the commandComponent would be the XML string stored as the value of the key event.cmd. The XML fragment after marshalling in this example would be: m xmlns=http:oracle.comrichClientcommk v=typesactionskm The m in the example means that this should be unmarshalled into a map. The k denotes the key and the value is of type String. On the server side, this XML fragment is parsed into a java.util.Map of one entry having type java.lang.String as the key and action java.lang.String as the value. The unmarshalled information is grouped per client ID, stored in the request map, and used when the components are being decoded. So in this example, when the commandButton is decoded, it will check for the presence of any client events using its client ID event.cmd and then queue an action event if one is found the decode behavior is implemented in the renderer hierarchy for commandButton component. Table 5–5 shows the mapping between corresponding JavaScript and Java types. Marshalling from Java to JavaScript happens mostly through JSON. This type of marshalling is straightforward as JSON is the object literal notation in JavaScript. The client-components usually have their properties encoded in JSON. Consider the following example: new AdfRichCommandButton’demoTemplate:richComand’ {’partialSubmit’:true,’useWindow’:false} The second argument {’partialSubmit’:true,’useWindow’:false} is a JSON object. There is no additional unmarshalling step required at the browser end as JSON can directly be parsed into the JavaScript environment as an object. Table 5–5 JavaScript to Java Type Map JavaScript Type Java Type Boolean java.lang.Boolean Number java.lang.Double String java.lang.String Date java.util.Date Array java.util.ArrayList Object java.util.Map 5-18 Web User Interface Developers Guide for Oracle Application Development Framework Encoding for a table also uses JSON to pass push messages to the client. The following is an example of an envelope containing a single encoded push message: [{rKey:0,type:update,data:[{val:Active Data Every Second: on row 0:78,prop:value,cInd:0},{val:Active Data Every Second: on row 0:78,prop:value,cInd:1}]}] The envelope is a JavaScript Array with only one object, which describes the message. This message contains information about the type of change, the actual value of the data, and so on, that is then used by the client-side table peer to update the table itself. Table 5–6 shows the mapping between corresponding Java and JavaScript types. Note that there could be some loss of information during the conversion process. For example, say you are using the following custom event to send the number 1 and the String test, as shown in the following example: AdfCustomEvent.queueevent.getSource, something, {first:1, second:test}; In the server-side listener, the type of the first parameter would become a java.lang.Double because numbers are converted to Doubles when going from JavaScript to Java. However, it might be that the parameter started on the server side as an int, and was converted to a number when conversion from Java to JavaScript took place. Now on its return trip to the server, it will be converted to a Double.

5.5 Executing a Script Within an Event Response

Using the ExtendedRenderKitService class, you can add JavaScript to an event response, for example, after invoking an action method binding. It can be a simple message like sending an alert informing the user that the database connection could not be established, or a call to a function like hide on a popup window to programatically dismiss a popup dialog. For example, in the File Explorer application, when the user clicks the UpOneFolder navigation button to move up in the folder structure, the folder pane is repainted to Table 5–6 Java to JavaScript Type Map Java Type JavaScript Type java.lang.Boolean Boolean java.lang.Double Number java.lang.Integer Number java.lang.Float Number java.lang.Long Number java.lang.Short Number java.lang.Character String java.lang.CharSequence String java.util.Collection Array java.util.Date Date java.util.Map Object Array Array java.awt.Color TrColor