Read ADF variables from a groovy file for more dynamic scenarios
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
ADF Support
This pattern can be applied on all versions of ADF 11g and ADF 12c.
The Idea
The idea is to:
- Extend last post‘s groovy file to add the new business logic.
- Extend the page to add the new customer type drop down menu
- Extend the managed bean to include customer type
- Run the application
- 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
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:
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
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