Saturday, 16 May 2015

ADF: How to create SelectOneChoice based on values from a database table in ADF Web Application - Part2

In my previous post, I have explained how to create the dropdown in the page with only one VO. In the OTN forum, I have noticed that some users have some issues when using that approach. In this post I will explain the Option 1 suggested in my previous post. ADF: How to create SelectOneChoice based on values from a database table in ADF Web Application - Part1

References :
   Ashish's Blog: Implementing custom search form in ADF programmatically
   Sireesha's Blog: Getting selected value from SelectOneChoice
  • Create a Query based View Object DeptVO with DepartmentId and DepartmentName.
  • Create another View Object DummyVO with the below Query. 
 select null DEPARTMENT_ID from dual  
  • Add List of Values to the DepartmentId Attribute of the DummyVO using the DeptVO.
  • Make Sure that you set the Updatable property of the Attribute to 'Always'.
  • Now you can just drop the DummyVO to the page as a ADF Form.
  • To get the values in the ValueChangeListener, you can use the below code (copied from the 2nd Reference mentioned above :)).
   public void valueChangeListener(ValueChangeEvent valueChangeEvent) {  
     System.out.println("Selected Value: "+valueChangeEvent.getNewValue());  
     this.setValueToEL("#{bindings.DepartmentId.inputValue}", valueChangeEvent.getNewValue()); //Updates the model  
     System.out.println("Selected Value: "+resolveExpression("#{bindings.DepartmentId.attributeValue}"));  
     System.out.println("Display Value: "+resolveExpression("#{bindings.DepartmentId.selectedValue ne ' ' ? bindings.DepartmentId.selectedValue.attributeValues[1] : ''}"));  
   }  
   public Object resolveExpression(String el) {     
     FacesContext facesContext = FacesContext.getCurrentInstance();  
     ELContext elContext = facesContext.getELContext();  
     ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();      
     ValueExpression valueExp = expressionFactory.createValueExpression(elContext,el,Object.class);  
     return valueExp.getValue(elContext);  
   }  
   public void setValueToEL(String el, Object val) {  
     FacesContext facesContext = FacesContext.getCurrentInstance();  
     ELContext elContext = facesContext.getELContext();  
     ExpressionFactory expressionFactory =  facesContext.getApplication().getExpressionFactory();  
     ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class);  
     exp.setValue(elContext, val);  
   }  
  • Now if you want set a default value for the dropdown, you can modify the query used for the DummyVO. For ex:- If you want to display 'Purchasing' as the default value for the dropdown and the corresponding DepartmentId is 30. Modify the query like below:
 select 30 DEPARTMENT_ID from dual  
  • Now when the page loads, the default value selected in the dropdown will be 'Purchasing'.
Sample application built using Jdev 12.1.3 can be found here.

Feel free to point out if anything is missing/wrong in this blog.

No comments:

Post a Comment