How to call JavaScript from a ADF Managed Bean

Postati in development, tutorial/howto con i tag , , , su 11/05/2011 da Matteo Formica

To run a JS function from our Managed Bean, first put the JS code in a <af:resource> component in our JSF page:

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:pe="http://xmlns.oracle.com/adf/pageeditor"
           xmlns:cust="http://xmlns.oracle.com/adf/faces/customizable"
           xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
<af:resource type="javascript">
    function myFunc() { [ any JS code ] };
</af:resource>

In the managed bean, put the following code in the appropriate method (e.g. in a action listener if we want to execute the JS code after a commandButton or commandLink is selected):

import javax.faces.context.FacesContext;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;
[…]
FacesContext facesContext = FacesContext.getCurrentInstance();
ExtendedRenderKitService service = (ExtendedRenderKitService) org.apache.myfaces.trinidad.util.Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
service.addScript(facesContext, "myFunc();");

Accessing an ADF Operation binding programmatically

Postati in development, tutorial/howto con i tag , , , su 11/05/2011 da Matteo Formica

Developing an ADF application, it sometimes comes out we need to invoke programmatically (e.g. from a Managed Bean) a method exposed as operation binding in a ADF Data Control. To do this, we need to access the current binding container:

OperationBinding searchOp = ADFUtils.findOperation("searchBusinessUnits");
The ADFUtils.findOperation() method has the following definition:
public static OperationBinding findOperation(String operationName) {
   OperationBinding op = getDCBindingContainer().getOperationBinding(operationName);
   if (op == null) {
      throw new RuntimeException("Operation '" + operationName +"' not found");
   }
   return op;
}

Unfortunately, when the previous code is executed in a method action invoked at page load time through an invokeAction binding, it happens that the reference to the binding container is null at this point. In this case, we need to access the binding container in a different way, using EL:

import oracle.adfinternal.view.faces.model.binding.FacesCtrlActionBinding;

[. . .]

FacesCtrlActionBinding searchOp = null;
searchOp = (FacesCtrlActionBinding)JSFUtils.resolveExpression("#{data.portal_peoplefinder_resultsPageDef.searchBusinessUnits}");
Map opParams = searchOp.getParamsMap();
opParams.put("searchTerm", searchString);
Object result = searchOp.execute();

The pageDef excerpt of the page (or page fragment) containing the binding I need to access (in this case peopleFinder_resultsPageDef.xml ):

<bindings>
 [...]
 <methodAction id="searchBusinessUnits" InstanceName="ContentServicesDC.dataProvider"
 DataControl="ContentServicesDC" RequiresUpdateModel="true"
 Action="invokeMethod" MethodName="searchBusinessUnits" IsViewObjectMethod="false"
 ReturnName="data.ContentServicesDC.methodResults.searchBusinessUnits_ContentServicesDC_dataProvider_searchBusinessUnits_result"/>
 [...]
 </bindings>

And the line of DataBindings.cpx regarding this binding:

<page id="portal_peoplefinder_resultsPageDef"
 path="oracle.webcenter.portalapp.pagefragments.peoplefinder_resultsPageDef"/>

IMPORTANT: the EL expression we are using to retrieve the binding must be comply with the following format:

[“data”] + [id of the corresponding <page> entry in DataBindings.cpx] + [name of the binding we want to retrieve]

Executing custom code at page load time via ADF Data Control

Postati in development, tutorial/howto con i tag , , , su 11/05/2011 da Matteo Formica

This is a very common use case: invocation of some custom code at page load time.

Thanks to ADF Data Control capability, we can incapsulate our custom code in a method of a POJO, and finally expose this as a Data Control (it’s worth reminding that it complies with JSR 227 specification, so it’s actually adherent to Java standards).

As we have many flavours of bindings, we are going to show in this tutorial how to achieve the same result using an attribute binding, and then using a method binding.

For this tutorial JDeveloper 11.1.1.5.0 has been used.

As a first step, let’s create a new Fusion Web Application (ADF) :

In the Model project we now create a new Java class, named PageLoadDC:


and expose this class as a Data Control:

After this, the Data Controls panel is populated with the new item:

And the DataControls.dcx file is updated with the following content:

Now let’s work on the ViewController project: at first we create a new JSF page, named home.jspx:

To make JDeveloper create the necessary bindings for you in the pageDef file for the new page, just drag & drop the method binding from the DataControls panel onto the page, choosing ADF Output Text as display mode for this binding:

In this way, many things happen behind the scenes:

–        a new pageDef for home.jspx page is created

–        a new binding for the method exposed in the data control is created

–        an iterator has been defined for this binding, to access the results of this method call.

The generated code in the newly created pageDef is:

Running the application gives the expected result:

An interesting observation:

Having named the DC method with a name starting with ‘get’ (in this case ‘getContentAtPageLoad’), this is interpreted by ADF as a getter of a property ‘contentAtPageLoad’, so the binding definition is really an attribute binding rather than a pure method binding, and in the home.jspx page the following EL expression is used to retrieve the value:

#{bindings.contentAtPageLoad.inputValue}

Now we add a new method in the DC, this time naming it in a different name, e.g. ‘buildContentAtPageLoad’:

and we expose the file as a DC again:

we can now notice how a method binding is actually created.

Now in the bindings box select the Plus icon to create a new binding, the select methodAction:

In the next screen just select the DC we just created, and the method name will appear in the ‘Operation’ dropdown list, then press OK:

As a result, a new binding is created in the pageDef file, and it appears in the ‘Bindings’ box in the page overview :

Now, to make this methodAction execute when the page is loaded, we just need to create an invokeAction to actually invoke it: in the ‘Executables’ panel in the ‘Bindings’ tab for the home.jspx page just click the Plus icon and select InvokeAction:

In the ‘InsertInvokeAction’ select the ‘buildContentAtPageLoad’ method and assign it an arbitrary ID, then click OK:

Then select the newly created invokeActionBinding, and in the property inspector set the ‘Refresh’ dropdown to ‘Always’:

In the JSPX page set the EL expression for the new binding like that:

Running the page again we get the expected result:

Easy as a pie.

You can download the example JDeveloper project here (remove the .pdf extension before unpacking).

Oracle plans for Java

Postati in news con i tag , , su 04/16/2011 da Matteo Formica

Here is an interesting article by Martijn Verburg and Ben Evans from TheServerSide. They spoke at the Scandinavian Developers Conference on Java 7 and polyglot programming on the JVM, and spent a long time talking to Henrik Stahl, the Oracle’s director of Java platform strategy – and an official spokesperson from Oracle. He’s able to pronounce on behalf of Oracle regarding Java. Henrik gave the authors many interesting insights.

Here the key points:

  • Oracle is very committed to Java and sees it as essential to their future.
  • An open, vibrant community is an essential part of that future, in fact it’s the community that’ll determine what standard technologies and techniques should go into the std platform (SE/EE).
  • The JVM will remain open and free forever, and additional technologies will be incorporated and open-sourced as appropriate.
  • Oracle is looking for willing partners in the community to engage and be part of the conversation – and they are listening.
Follow

Get every new post delivered to your Inbox.