Select one choice default selection

July 30th, 2010 | Posted by in Oracle ADF | 1 Comment

When you create an entity, like an employee, and one attribute of that entity is tied to a list of values you usualy use a SelectOneChoice component for that attribute. That way you are going to get for that attribute only the values you expose trough the SelectOneChioce component.

But there is a usability problem when the above mentioned attribute is required because even if you uncheck the Include "No Selection" Item when you create the entity you will see a blank selection in the SelectOneChioce component.

Let’s take a practical example so that we have a more accurate view of the problem and the proposed solution.

A common case is the relation between employees and departments in the HR schema. Assume that a department is required for each employee.

When we create an employee we are going to make a list of values from the department names and use it as a SelectOneChoice component for populating the DepartmentID attribute of the employee.

I created a LOV on the DepartmentId attribute

and unchecked the Include "No Selection" Item.

In the page for creating the employee I created a form with the EmployeeView attributes and the input for DepartmentId is a SelectOneChoice component.
Now if the DepartmentId is a mandatory attribute and we do not choose a department when we try to save the employee we get an error message.

To make the first department the default choice for every new employee we are going to get the SelectOneChoice component with the ‘socDepartment’ id in a backing bean and set it’s value to the first department. The code for that is like this:

import javax.faces.event.PhaseEvent;
import ro.gebs.soc.view.util.JSFUtils;

public class EmpBean {

  public void makeASelection(PhaseEvent phaseEvent) {
    RichSelectOneChoice soc;
    soc = (RichSelectOneChoice)JSFUtils.findComponentInRoot("socDepartment");
    if (soc != null && soc.getValue() == null)
      soc.setValue(new Integer(0));

The method uses the utility class JSFUtils created by Duncan Mills and Steve Muench.

In order to execute that method we call it in the beforePhase of the view.

<f:view beforePhase="#{empBean.makeASelection}">

That is all we need in order to have a default selected department.

The source code for the sample application can be downloaded from here.

Tags: ,

One Response to “Select one choice default selection”

  1. david says:

    this method can’t be used in page fragment. It gets null pointer exception somehow. Any help ?

Leave a Reply