Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label Managed bean. Show all posts
Showing posts with label Managed bean. Show all posts

Thursday, 3 January 2019

Using Bean Data Control in Oracle ADF

 This post talks about a basic requirement that is creating bean data control in an ADF Application. Here I'll show you how to create POJO based Java class and use it as data control to populate data on the page.

Let's start by creating a fusion web application with a model and view controller project


Now create a POJO class PersonDetail in the model project, This class represents a person and captures his details like name, date of birth and phone number.

  1. package beandatacontrol.model.bean;
  2. public class PersonDetail {
  3. public PersonDetail() {
  4. // TODO Auto-generated constructor stub
  5. }
  6. public PersonDetail(String name, String phoneNo, String dateOfBirth) {
  7. this.name = name;
  8. this.phoneNo = phoneNo;
  9. this.dateOfBirth = dateOfBirth;
  10. }
  11. private String name;
  12. private String phoneNo;
  13. private String dateOfBirth;
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setPhoneNo(String phoneNo) {
  21. this.phoneNo = phoneNo;
  22. }
  23. public String getPhoneNo() {
  24. return phoneNo;
  25. }
  26. public void setDateOfBirth(String dateOfBirth) {
  27. this.dateOfBirth = dateOfBirth;
  28. }
  29. public String getDateOfBirth() {
  30. return dateOfBirth;
  31. }
  32. }

The second class is Society that holds the list of people and it goes like this.

  1. package beandatacontrol.model.bean;
  2. import java.util.ArrayList;
  3. import oracle.binding.AttributeContext;
  4. import oracle.binding.RowContext;
  5. public class Society {
  6. public ArrayList<PersonDetail> person = new ArrayList<PersonDetail>();
  7. public Society() {
  8. super();
  9. if (person.size() == 0) {
  10. person.add(new PersonDetail("Ashish Awasthi", "+91672938", "02/01/1991"));
  11. person.add(new PersonDetail("Shyam Kumar", "+91345545343", "06/09/1991"));
  12. person.add(new PersonDetail("Alex Smith", "+458954638", "08/02/1991"));
  13. person.add(new PersonDetail("Raghav Mani", "+914512698", "09/06/1991"));
  14. person.add(new PersonDetail("Ahmad Sheikh", "+7459654245", "12/05/1991"));
  15. }
  16. }
  17. public void setPerson(ArrayList<PersonDetail> person) {
  18. this.person = person;
  19. }
  20. public ArrayList<PersonDetail> getPerson() {
  21. return person;
  22. }
  23. }

Now right click on Society class and select Create Data Control


In the next step, I have selected Custom CRUD operation support in ADF Data Control Features


Click on Next and don't change access mode, the default value is Scrollable

 


Click on Finish and you can see all the details


Now you can see a DataControl.dcx file is created in the project, In short bean data control is ready for use.


Right-click on the view controller project and create a page and drop person data control on the page as ADF table. Also, drop create operation as a button and run the application.


Check to create button, click on that and see a new row added in the ADF table.


Cheers 🙂 Happy Learning

Friday, 30 November 2018

Switching ADF Skin at run time using trinidad-config.xml

 This post talks about switching ADF skin at runtime and uses a managed bean variable to change skin name at runtime. Sometimes we want to use multiple themes for our application and in that case, we need this.

Created an ADF application, 2 different skins and a page in view controller project


Learn more about creating ADF skin in JDeveloper 

This page has a button to switch between skins and an outputText. Create a variable in the managed bean to hold skin name and button action listener to change skin variable value.

Here goes the managed bean code

  1. package switchadfskin.view.bean;
  2. import javax.faces.application.ViewHandler;
  3. import javax.faces.component.UIViewRoot;
  4. import javax.faces.context.FacesContext;
  5. import javax.faces.event.ActionEvent;
  6. public class SwitchADFSkinBean {
  7. public SwitchADFSkinBean() {
  8. }
  9. //Set default skin name
  10. private String skinName = "RedButton";
  11. public void setSkinName(String skinName) {
  12. this.skinName = skinName;
  13. }
  14. public String getSkinName() {
  15. return skinName;
  16. }
  17. /**Method to change skin at runtime
  18. * @param actionEvent
  19. */
  20. public void switchSkinAction(ActionEvent actionEvent) {
  21. //Change skin name
  22. if (skinName.equalsIgnoreCase("RedButton")) {
  23. this.setSkinName("GreenButton");
  24. } else {
  25. this.setSkinName("RedButton");
  26. }
  27. //Reload page
  28. refreshPage();
  29. }
  30. /**Method to refresh/reload page
  31. */
  32. protected void refreshPage() {
  33. FacesContext fctx = FacesContext.getCurrentInstance();
  34. String page = fctx.getViewRoot().getViewId();
  35. ViewHandler ViewH = fctx.getApplication().getViewHandler();
  36. UIViewRoot UIV = ViewH.createView(fctx, page);
  37. UIV.setViewId(page);
  38. fctx.setViewRoot(UIV);
  39. }
  40. }

The next step is to change the trinidad-config.xml file, set skin-family variable to refer to its value from managed bean so that when the user changes the bean variable then skin (CSS) is updated in the application.

  1. <?xml version="1.0" encoding="windows-1252"?>
  2. <trinidad-config xmlns="http://myfaces.apache.org/trinidad/config">
  3. <skin-family>#{SwitchADFSkinBean.skinName}</skin-family>
  4. </trinidad-config>

Now run and check application

The default skin is "RedButton" so it looks like this


and on click of the button

Sample ADF Application (JDeveloper 12.1.3) - Download

Cheers 🙂 Happy Learning