Read ADF variables from a groovy file for more dynamic scenarios

Amr Gawish
kblog
Published in
3 min readJun 18, 2015

--

In the last post we were able to execute a simple groovy file to change discount value of a simple application, but this is very basic and sometimes there are multiple other parameters that you want to consider first before returning a discount value like for instance customer type. In this post I'll show you how can groovy read a request variable (customer type) and take a decision based on that parameter value.

The end result

End result of running the application, notice how the groovy can get the Customer Type information

ADF Support

This pattern can be applied on all versions of ADF 11g and ADF 12c.

The Idea

The idea is to:

  1. Extend last post‘s groovy file to add the new business logic.
  2. Extend the page to add the new customer type drop down menu
  3. Extend the managed bean to include customer type
  4. Run the application
  5. Enjoy your tea or coffee!

The Implementation

First We need to make sure that JSF runtime is available in Groovy classpath. Groovy classpath are usually the same classpath defined for weblogic, so we need to make sure to add the the JSF libraries inside setDomainEnv.cmd or setDomainEnv.sh (depending on your OS), but adding it to the variable POST_CLASSPATH like so

Adding JSF to Classpath

After that restart weblogic, and you should be good to go.

Next let’s create the Groovy method that can resolve expression, you can copy and paste normal java method like so:

Resolve Expression method inside groovy file

Make sure that you add the right imports at the top of the groovy file.

Now, we add a new String attribute in DiscountBean java class and map it to a selectOneChoice inside discount.jspx page

The new Select One Choice

Now we run the page, and see the magic happens!

Download the code

Check it on Github.

Taking this further

As you can notice, Groovy has access to request scope, not only that but it shares the same FacesContext as ADF does, which means you can do more than just getting requestScope variables

# That’s all folks

--

--

Editor for

A father, Gamification advocate, and a fellow software craftsman!