Hooking Up an ADF Faces Select Component with a Managed Bean

This is a guest post by my coworker Jeanne Waldman

Here is a simple example showing how to set up an ADF Faces select component using a managed bean. ADF Faces has many select components, like selectOneChoice, selectOneListbox, selectOneCheckbox, selectManyCheckbox, selectManyShuttle, etc, and you set them all up essentially the same way - you bind the value of a selectOne to an Object and a selectMany to a List.

The select component's value is the List of selected items. The select component's f:selectItems are all the possible items the user can select.

selectManyCheckboxChecked.png

The Code

Jspx code:

  1 <af:selectManyCheckbox label="Locations" id="smc1"
  2                        value="#{sessionScope.weatherBean.locationsSelected}">
  3   <f:selectItems value="#{sessionScope.weatherBean.locationSelectItems}"
  4                  id="si1"/>
  5 </af:selectManyCheckbox>

faces-config.xml:

  1 <managed-bean> 
  2   <managed-bean-name>weatherBean</managed-bean-name> 
  3   <managed-bean-class>sprinkles.view.bean.weather.data.WeatherBean</managed-bean-class> 
  4   <managed-bean-scope>session</managed-bean-scope> 
  5 </managed-bean>

Managed bean code:

  1 package sprinkles.view.bean.weather.data; 
  2  
  3 import java.util.ArrayList; 
  4 import java.util.Collections; 
  5 import java.util.List; 
  6 import javax.faces.model.SelectItem; 
  7 import org.apache.myfaces.trinidad.component.UIXSelectMany;
  8  
  9  
 10  
 11 public class WeatherBean
 12 { 
 13   public WeatherBean() 
 14   { 
 15     super(); 
 16   } 
 17  
 18   /** 
 19    * This lists ALL the locations in a SelectItem List so that it can be displayed 
 20    * as a selectManyCheckbox. 
 21    * The f:selectItems value is bound to this list. 
 22    * @return List of SelectItems 
 23    */ 
 24   public List<SelectItem> getLocationSelectItems()
 25   { 
 26     List<String> locations = _getLocations(); 
 27     List<SelectItem> locationsSelectItems = new ArrayList<SelectItem>();
 28     for (String location: locations) 
 29     { 
 30       // the parameters to the constructor are value, label, description
 31       SelectItem item = new SelectItem(location, location, null);
 32       locationsSelectItems.add(item); 
 33     } 
 34     return locationsSelectItems; 
 35   } 
 36  
 37   /** 
 38    * The selectMany component's value is bound to this. This sets what is selected.
 39    * @param value 
 40    */ 
 41   public void setLocationsSelected(List<String> value)
 42   { 
 43     _locationsSelected = value; 
 44   } 
 45  
 46   /** 
 47    * The selectMany component's value is bound to this. This returns what was selected.
 48    * @return List<String> list of the locations that are selected in the selectMany 
 49    * component. 
 50    */ 
 51   public List<String> getLocationsSelected()
 52   { 
 53     // If you would like to default your select component to something, you would do it here
 54     // by checking if the _locationsSelected is null, and if so, you'd return a List with 
 55     // your default value 
 56     return _locationsSelected; 
 57   } 
 58  
 59  
 60   private List<String> _getLocations()
 61   { 
 62     // you fill this in with your selection list.
 63     // In this example the List contains "Belmont", "San Jose", "Truckee", "Boulder".
 64     return yourList; 
 65   } 
 66  
 67   private List<String> _locationsSelected;
 68 }

Additional Documentation

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

An exploration in the lighter side of ADF development.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today